lenso-process-protocol 0.2.0

Strict shared protocol values and conformance helpers for Lenso Execution Adapters.
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
//! Runtime-neutral Authoring V2 values and validation.

use std::collections::{BTreeMap, BTreeSet};

use serde::{Deserialize, Serialize, de::DeserializeOwned};
use serde_json::Value;

use super::{InvocationExtension, ProtocolError, VALUE_PROFILE};

/// First interoperable Authoring value API.
pub const AUTHORING_API_VERSION: u32 = 2;
/// Default maximum encoded frame size.
pub const DEFAULT_MAX_FRAME_BYTES: u64 = 1_048_576;

/// Decodes one bounded strict Authoring value.
pub fn decode_frame<T: DeserializeOwned>(
    wire: &[u8],
    max_frame_bytes: u64,
) -> Result<T, ProtocolError> {
    if u64::try_from(wire.len()).map_or(true, |length| length > max_frame_bytes) {
        return Err(error("Authoring frame exceeds max_frame_bytes"));
    }
    super::decode_strict(wire)
}

/// Immutable identity of one admitted Plugin generation.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SessionIdentity {
    pub session: String,
    pub plugin_instance: String,
    pub plugin_generation: String,
    pub artifact_digest: String,
    pub contract_digest: String,
    pub runtime_profile: String,
    pub value_profile: String,
}

/// Host bounds confirmed during initialization.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuthoringLimits {
    pub max_frame_bytes: u64,
    pub max_active_invocations: u32,
    pub max_active_outbound_calls: u32,
    pub max_queued_calls: u32,
    pub max_unfinished_executions: u32,
    pub max_retired_ids: u32,
}

impl AuthoringLimits {
    #[must_use]
    pub const fn defaults() -> Self {
        Self {
            max_frame_bytes: DEFAULT_MAX_FRAME_BYTES,
            max_active_invocations: 32,
            max_active_outbound_calls: 32,
            max_queued_calls: 32,
            max_unfinished_executions: 32,
            max_retired_ids: 1_024,
        }
    }
}

/// Cardinality of one named requirement.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RequirementCardinality {
    One,
    Optional,
    Many,
}

/// One source-declared dependency, keyed independently from its Capability.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RequirementDeclaration {
    pub requirement_id: String,
    pub capability_id: String,
    pub descriptor_version: String,
    pub descriptor_digest: String,
    pub cardinality: RequirementCardinality,
}

/// One exact provider selected for a named requirement.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RouteDescriptor {
    pub route_id: String,
    pub requirement_id: String,
    pub capability_id: String,
    pub descriptor_version: String,
    pub descriptor_digest: String,
    pub provider_instance: String,
    pub provider_order: u32,
}

/// One endpoint exported by the admitted Plugin instance.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ProvidedEndpoint {
    pub endpoint_id: String,
    pub capability_id: String,
    pub descriptor_version: String,
    pub descriptor_digest: String,
}

/// Host-to-runtime initialization value.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct InitializeParams {
    pub api_version: u32,
    pub identity: SessionIdentity,
    pub config: Value,
    pub required_declarations: Vec<RequirementDeclaration>,
    pub routes: Vec<RouteDescriptor>,
    pub provided_endpoints: Vec<ProvidedEndpoint>,
    pub limits: AuthoringLimits,
}

/// Runtime echo proving that it admitted exactly the Host initialization.
pub type InitializedResult = InitializeParams;

/// Host-to-runtime factory invocation.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ConstructParams {
    pub session: String,
    pub lifecycle_scope_id: String,
    pub remaining_budget_nanos: String,
}

/// Result of transferring the constructed object to the runtime.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum FactoryOutcome {
    Constructed,
    Failed { detail: String },
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ConstructedResult {
    pub session: String,
    pub lifecycle_scope_id: String,
    pub outcome: FactoryOutcome,
}

/// Invocation ancestry and ambient values that outbound calls must preserve.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct InvocationScope {
    pub scope_id: String,
    pub parent_scope_id: Option<String>,
    pub remaining_budget_nanos: String,
    pub permissions: Vec<String>,
    pub extensions: Vec<InvocationExtension>,
}

/// Host-to-plugin operation invocation.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct InvokeParams {
    pub session: String,
    pub correlation_id: String,
    pub endpoint_id: String,
    pub capability_id: String,
    pub descriptor_version: String,
    pub descriptor_digest: String,
    pub operation: String,
    pub scope: InvocationScope,
    pub payload: Value,
}

/// Portable operation outcome. It is distinct from execution settlement.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum RuntimeFailure {
    Unavailable {
        capability: String,
    },
    UnknownOperation {
        capability: String,
        operation: String,
    },
    AmbiguousBinding {
        capability: String,
        providers: u32,
    },
    ProtocolViolation {
        capability: String,
    },
    MissingPluginFactory {
        instance: String,
        package_id: String,
    },
    UnavailableExecutionClass {
        instance_key: String,
        execution_class: String,
    },
    InvalidResolvedPlan {
        detail: String,
    },
    AdmissionClosed,
    ResourceExhausted {
        capability: String,
        operation: String,
    },
    DeadlineExceeded {
        request_id: String,
    },
    Cancelled {
        request_id: String,
    },
    Internal {
        detail: String,
    },
    PluginFailure {
        detail: String,
    },
    PluginRestartExhausted {
        instance: String,
        attempts: u32,
    },
}

impl RuntimeFailure {
    pub fn validate(&self) -> Result<(), ProtocolError> {
        match self {
            Self::Unavailable { capability } | Self::ProtocolViolation { capability } => {
                validate_token("capability", capability)
            }
            Self::UnknownOperation {
                capability,
                operation,
            }
            | Self::ResourceExhausted {
                capability,
                operation,
            } => {
                validate_token("capability", capability)?;
                validate_token("operation", operation)
            }
            Self::AmbiguousBinding {
                capability,
                providers,
            } => {
                validate_token("capability", capability)?;
                validate_limit("providers", u64::from(*providers), 1_048_576)
            }
            Self::MissingPluginFactory {
                instance,
                package_id,
            } => {
                validate_token("instance", instance)?;
                validate_token("package_id", package_id)
            }
            Self::UnavailableExecutionClass {
                instance_key,
                execution_class,
            } => {
                validate_token("instance_key", instance_key)?;
                validate_token("execution_class", execution_class)
            }
            Self::InvalidResolvedPlan { detail }
            | Self::Internal { detail }
            | Self::PluginFailure { detail } => validate_detail(detail),
            Self::AdmissionClosed => Ok(()),
            Self::DeadlineExceeded { request_id } | Self::Cancelled { request_id } => {
                validate_decimal("request_id", request_id).map(|_| ())
            }
            Self::PluginRestartExhausted { instance, attempts } => {
                validate_token("instance", instance)?;
                validate_limit("attempts", u64::from(*attempts), 1_048_576)
            }
        }
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum InvocationOutcome {
    Success { value: Value },
    Domain { error: Value },
    Runtime { failure: RuntimeFailure },
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct InvocationResult {
    pub session: String,
    pub correlation_id: String,
    pub outcome: InvocationOutcome,
}

/// Plugin-to-Host call through one exact Plan route.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OutboundCallParams {
    pub session: String,
    pub correlation_id: String,
    pub requirement_id: String,
    pub route_id: String,
    pub operation: String,
    pub scope: InvocationScope,
    pub payload: Value,
}

pub type OutboundCallResult = InvocationResult;

/// Cancellation request. Acknowledgement does not imply execution settlement.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CancelParams {
    pub session: String,
    pub scope_id: String,
    pub correlation_id: String,
    pub reason: String,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CancelAck {
    pub session: String,
    pub scope_id: String,
    pub correlation_id: String,
    pub accepted: bool,
}

/// Runtime observation that one execution has actually stopped.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Settlement {
    pub session: String,
    pub scope_id: String,
    pub correlation_id: String,
    pub state: SettlementState,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SettlementState {
    Completed,
    Cancelled,
    Abandoned,
}

/// Fresh cleanup scope with the remaining shared cleanup budget.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct StopParams {
    pub session: String,
    pub cleanup_scope_id: String,
    pub remaining_budget_nanos: String,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CleanupDiagnostic {
    pub code: String,
    pub detail: String,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct StoppedResult {
    pub session: String,
    pub cleanup_scope_id: String,
    pub hook: StopHookOutcome,
    pub diagnostics: Vec<CleanupDiagnostic>,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum StopHookOutcome {
    NotDeclared,
    Completed,
    Failed,
}

impl SessionIdentity {
    pub fn validate(&self) -> Result<(), ProtocolError> {
        validate_token("session", &self.session)?;
        validate_token("plugin_instance", &self.plugin_instance)?;
        validate_decimal("plugin_generation", &self.plugin_generation)?;
        validate_digest("artifact_digest", &self.artifact_digest)?;
        validate_digest("contract_digest", &self.contract_digest)?;
        validate_token("runtime_profile", &self.runtime_profile)?;
        if self.value_profile != VALUE_PROFILE {
            return Err(error("unsupported portable value profile"));
        }
        Ok(())
    }
}

impl AuthoringLimits {
    pub fn validate(&self) -> Result<(), ProtocolError> {
        validate_limit(
            "max_frame_bytes",
            self.max_frame_bytes,
            DEFAULT_MAX_FRAME_BYTES,
        )?;
        validate_limit(
            "max_active_invocations",
            u64::from(self.max_active_invocations),
            1_024,
        )?;
        validate_limit(
            "max_active_outbound_calls",
            u64::from(self.max_active_outbound_calls),
            1_024,
        )?;
        validate_limit("max_queued_calls", u64::from(self.max_queued_calls), 65_536)?;
        validate_limit(
            "max_unfinished_executions",
            u64::from(self.max_unfinished_executions),
            65_536,
        )?;
        validate_limit(
            "max_retired_ids",
            u64::from(self.max_retired_ids),
            1_048_576,
        )
    }
}

impl InitializeParams {
    /// Validates initialization and exact requirement-to-route bindings.
    pub fn validate(&self) -> Result<(), ProtocolError> {
        if self.api_version != AUTHORING_API_VERSION {
            return Err(error("unsupported Authoring API version"));
        }
        self.identity.validate()?;
        self.limits.validate()?;
        validate_sorted(
            &self.required_declarations,
            |value| &value.requirement_id,
            "required_declarations",
        )?;
        validate_sorted(
            &self.provided_endpoints,
            |value| &value.endpoint_id,
            "provided_endpoints",
        )?;

        let mut declarations = BTreeMap::new();
        for declaration in &self.required_declarations {
            declaration.validate()?;
            declarations.insert(declaration.requirement_id.as_str(), declaration);
        }
        let mut counts = BTreeMap::<&str, usize>::new();
        let mut next_orders = BTreeMap::<&str, u32>::new();
        let mut route_ids = BTreeSet::new();
        let mut providers = BTreeSet::new();
        let mut previous_route: Option<(&str, u32, &str)> = None;
        for route in &self.routes {
            route.validate()?;
            if !route_ids.insert(route.route_id.as_str()) {
                return Err(error("route_id must be unique within a session"));
            }
            let sort_key = (
                route.requirement_id.as_str(),
                route.provider_order,
                route.provider_instance.as_str(),
            );
            if previous_route.is_some_and(|previous| previous >= sort_key) {
                return Err(error(
                    "routes must be sorted by requirement_id, provider_order and provider_instance",
                ));
            }
            previous_route = Some(sort_key);
            let declaration = declarations
                .get(route.requirement_id.as_str())
                .ok_or_else(|| error("route references an unknown requirement_id"))?;
            if route.capability_id != declaration.capability_id
                || route.descriptor_version != declaration.descriptor_version
                || route.descriptor_digest != declaration.descriptor_digest
            {
                return Err(error(
                    "route descriptor does not match its named requirement",
                ));
            }
            let expected = next_orders
                .entry(route.requirement_id.as_str())
                .or_default();
            if route.provider_order != *expected {
                return Err(error(
                    "requirement routes must use contiguous provider_order values",
                ));
            }
            if !providers.insert((
                route.requirement_id.as_str(),
                route.provider_instance.as_str(),
            )) {
                return Err(error("one requirement cannot bind the same provider twice"));
            }
            *expected += 1;
            *counts.entry(route.requirement_id.as_str()).or_default() += 1;
        }
        for declaration in &self.required_declarations {
            let count = counts
                .get(declaration.requirement_id.as_str())
                .copied()
                .unwrap_or_default();
            match declaration.cardinality {
                RequirementCardinality::One if count != 1 => {
                    return Err(error("one requirement must bind exactly one route"));
                }
                RequirementCardinality::Optional if count > 1 => {
                    return Err(error("optional requirement may bind at most one route"));
                }
                RequirementCardinality::Many
                | RequirementCardinality::One
                | RequirementCardinality::Optional => {}
            }
        }
        for endpoint in &self.provided_endpoints {
            endpoint.validate()?;
        }
        Ok(())
    }

    /// Validates initialization for one exact Adapter-selected runtime profile.
    pub fn validate_for_runtime_profile(&self, expected: &str) -> Result<(), ProtocolError> {
        self.validate()?;
        if self.identity.runtime_profile != expected {
            return Err(error(
                "runtime profile does not match the selected Adapter profile",
            ));
        }
        Ok(())
    }

    /// Requires an exact initialized echo; partial admission is forbidden.
    pub fn validate_initialized(
        &self,
        initialized: &InitializedResult,
    ) -> Result<(), ProtocolError> {
        initialized.validate()?;
        if initialized != self {
            return Err(error(
                "initialized result does not exactly echo initialization",
            ));
        }
        Ok(())
    }
}

impl RequirementDeclaration {
    fn validate(&self) -> Result<(), ProtocolError> {
        validate_requirement_id(&self.requirement_id)?;
        validate_descriptor(
            &self.capability_id,
            &self.descriptor_version,
            &self.descriptor_digest,
        )
    }
}

impl RouteDescriptor {
    fn validate(&self) -> Result<(), ProtocolError> {
        validate_token("route_id", &self.route_id)?;
        validate_requirement_id(&self.requirement_id)?;
        validate_token("provider_instance", &self.provider_instance)?;
        validate_descriptor(
            &self.capability_id,
            &self.descriptor_version,
            &self.descriptor_digest,
        )
    }
}

impl ProvidedEndpoint {
    fn validate(&self) -> Result<(), ProtocolError> {
        validate_token("endpoint_id", &self.endpoint_id)?;
        validate_descriptor(
            &self.capability_id,
            &self.descriptor_version,
            &self.descriptor_digest,
        )
    }
}

impl InvocationScope {
    pub fn validate(&self) -> Result<(), ProtocolError> {
        validate_token("scope_id", &self.scope_id)?;
        if let Some(parent) = &self.parent_scope_id {
            validate_token("parent_scope_id", parent)?;
        }
        validate_decimal("remaining_budget_nanos", &self.remaining_budget_nanos)?;
        validate_sorted_strings(&self.permissions, "permissions")?;
        let mut keys = BTreeSet::new();
        for extension in &self.extensions {
            extension.validate()?;
            if !keys.insert(extension.key.as_str()) {
                return Err(error("extensions must be sorted and unique"));
            }
        }
        if self
            .extensions
            .windows(2)
            .any(|pair| pair[0].key >= pair[1].key)
        {
            return Err(error("extensions must be sorted and unique"));
        }
        Ok(())
    }

    /// Validates an outbound child scope against its parent invocation.
    pub fn validate_child_of(&self, parent: &Self) -> Result<(), ProtocolError> {
        self.validate()?;
        parent.validate()?;
        if self.parent_scope_id.as_deref() != Some(parent.scope_id.as_str()) {
            return Err(error("outbound scope must name its exact parent scope"));
        }
        if decimal_value(&self.remaining_budget_nanos)?
            > decimal_value(&parent.remaining_budget_nanos)?
        {
            return Err(error("outbound remaining budget may not increase"));
        }
        if self.permissions != parent.permissions || self.extensions != parent.extensions {
            return Err(error(
                "outbound scope must preserve permissions and extensions",
            ));
        }
        Ok(())
    }
}

impl ConstructParams {
    pub fn validate_for(&self, identity: &SessionIdentity) -> Result<(), ProtocolError> {
        validate_session(&self.session, identity)?;
        validate_token("lifecycle_scope_id", &self.lifecycle_scope_id)?;
        validate_decimal("remaining_budget_nanos", &self.remaining_budget_nanos).map(|_| ())
    }
}
impl ConstructedResult {
    pub fn validate_for(&self, request: &ConstructParams) -> Result<(), ProtocolError> {
        validate_echo(
            &self.session,
            &self.lifecycle_scope_id,
            &request.session,
            &request.lifecycle_scope_id,
        )?;
        if let FactoryOutcome::Failed { detail } = &self.outcome {
            validate_detail(detail)?;
        }
        Ok(())
    }
}
impl InvokeParams {
    pub fn validate_for(&self, identity: &SessionIdentity) -> Result<(), ProtocolError> {
        validate_session(&self.session, identity)?;
        validate_decimal("correlation_id", &self.correlation_id)?;
        validate_token("endpoint_id", &self.endpoint_id)?;
        validate_descriptor(
            &self.capability_id,
            &self.descriptor_version,
            &self.descriptor_digest,
        )?;
        validate_token("operation", &self.operation)?;
        self.scope.validate()
    }
}
impl InvokeParams {
    /// Validates the endpoint identity against the admitted initialization.
    pub fn validate_against(&self, initialize: &InitializeParams) -> Result<(), ProtocolError> {
        self.validate_for(&initialize.identity)?;
        let endpoint = initialize
            .provided_endpoints
            .iter()
            .find(|endpoint| endpoint.endpoint_id == self.endpoint_id)
            .ok_or_else(|| error("invoke references an unknown endpoint_id"))?;
        if endpoint.capability_id != self.capability_id
            || endpoint.descriptor_version != self.descriptor_version
            || endpoint.descriptor_digest != self.descriptor_digest
        {
            return Err(error(
                "invoke descriptor does not match its admitted endpoint",
            ));
        }
        Ok(())
    }
}
impl InvocationResult {
    pub fn validate_for(&self, request: &InvokeParams) -> Result<(), ProtocolError> {
        validate_echo(
            &self.session,
            &self.correlation_id,
            &request.session,
            &request.correlation_id,
        )?;
        if let InvocationOutcome::Runtime { failure } = &self.outcome {
            failure.validate()?;
        }
        Ok(())
    }

    /// Validates an outbound-call result against its exact request identity.
    pub fn validate_for_outbound(&self, request: &OutboundCallParams) -> Result<(), ProtocolError> {
        validate_echo(
            &self.session,
            &self.correlation_id,
            &request.session,
            &request.correlation_id,
        )?;
        if let InvocationOutcome::Runtime { failure } = &self.outcome {
            failure.validate()?;
        }
        Ok(())
    }
}
impl OutboundCallParams {
    pub fn validate_for(
        &self,
        identity: &SessionIdentity,
        parent: &InvocationScope,
    ) -> Result<(), ProtocolError> {
        validate_session(&self.session, identity)?;
        validate_decimal("correlation_id", &self.correlation_id)?;
        validate_requirement_id(&self.requirement_id)?;
        validate_token("route_id", &self.route_id)?;
        validate_token("operation", &self.operation)?;
        self.scope.validate_child_of(parent)
    }
}
impl OutboundCallParams {
    /// Validates the opaque route against this exact session and named requirement.
    pub fn validate_against(
        &self,
        initialize: &InitializeParams,
        parent: &InvocationScope,
        parent_active: bool,
    ) -> Result<(), ProtocolError> {
        self.validate_for(&initialize.identity, parent)?;
        if !parent_active {
            return Err(error("closed parent scope cannot start an outbound call"));
        }
        let route = initialize
            .routes
            .iter()
            .find(|route| route.route_id == self.route_id)
            .ok_or_else(|| error("outbound call references an unknown route_id"))?;
        if route.requirement_id != self.requirement_id {
            return Err(error("outbound route belongs to another requirement"));
        }
        Ok(())
    }
}
impl CancelParams {
    pub fn validate_for(&self, identity: &SessionIdentity) -> Result<(), ProtocolError> {
        validate_session(&self.session, identity)?;
        validate_token("scope_id", &self.scope_id)?;
        validate_decimal("correlation_id", &self.correlation_id)?;
        validate_detail(&self.reason)
    }
}
impl CancelAck {
    pub fn validate_for(&self, request: &CancelParams) -> Result<(), ProtocolError> {
        validate_echo(
            &self.session,
            &self.scope_id,
            &request.session,
            &request.scope_id,
        )?;
        if self.correlation_id != request.correlation_id || !self.accepted {
            return Err(error("cancel acknowledgement mismatch"));
        }
        Ok(())
    }
}
impl Settlement {
    pub fn validate_for(&self, identity: &SessionIdentity) -> Result<(), ProtocolError> {
        validate_session(&self.session, identity)?;
        validate_token("scope_id", &self.scope_id)?;
        validate_decimal("correlation_id", &self.correlation_id).map(|_| ())
    }
}
impl StopParams {
    pub fn validate_for(&self, identity: &SessionIdentity) -> Result<(), ProtocolError> {
        validate_session(&self.session, identity)?;
        validate_token("cleanup_scope_id", &self.cleanup_scope_id)?;
        validate_decimal("remaining_budget_nanos", &self.remaining_budget_nanos).map(|_| ())
    }
}
impl StoppedResult {
    pub fn validate_for(&self, request: &StopParams) -> Result<(), ProtocolError> {
        validate_echo(
            &self.session,
            &self.cleanup_scope_id,
            &request.session,
            &request.cleanup_scope_id,
        )?;
        for diagnostic in &self.diagnostics {
            validate_token("cleanup diagnostic code", &diagnostic.code)?;
            validate_detail(&diagnostic.detail)?;
        }
        Ok(())
    }
}

fn validate_descriptor(capability: &str, version: &str, digest: &str) -> Result<(), ProtocolError> {
    validate_token("capability_id", capability)?;
    validate_token("descriptor_version", version)?;
    validate_digest("descriptor_digest", digest)
}
fn validate_session(session: &str, identity: &SessionIdentity) -> Result<(), ProtocolError> {
    if session != identity.session {
        return Err(error("message session does not match admitted session"));
    }
    Ok(())
}
fn validate_echo(
    session: &str,
    id: &str,
    expected_session: &str,
    expected_id: &str,
) -> Result<(), ProtocolError> {
    if session != expected_session || id != expected_id {
        return Err(error("response identity mismatch"));
    }
    Ok(())
}
fn validate_detail(value: &str) -> Result<(), ProtocolError> {
    if value.is_empty() || value.len() > 1_024 {
        return Err(error("detail must contain 1..=1024 bytes"));
    }
    Ok(())
}
fn validate_limit(name: &str, value: u64, maximum: u64) -> Result<(), ProtocolError> {
    if value == 0 || value > maximum {
        return Err(error(format!("{name} must be within 1..={maximum}")));
    }
    Ok(())
}
fn validate_token(name: &str, value: &str) -> Result<(), ProtocolError> {
    if value.is_empty()
        || value.len() > 256
        || !value.bytes().all(|byte| {
            byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'_' | b'-' | b'@' | b'/' | b':')
        })
    {
        return Err(error(format!("{name} must be a portable token")));
    }
    Ok(())
}
fn validate_requirement_id(value: &str) -> Result<(), ProtocolError> {
    if value.is_empty()
        || value.len() > 64
        || !value.as_bytes()[0].is_ascii_lowercase()
        || !value
            .bytes()
            .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'_')
    {
        return Err(error("requirement_id must match ^[a-z][a-z0-9_]{0,63}$"));
    }
    Ok(())
}
fn validate_digest(name: &str, value: &str) -> Result<(), ProtocolError> {
    let Some(hex) = value.strip_prefix("sha256:") else {
        return Err(error(format!("{name} must be a canonical SHA-256 digest")));
    };
    if hex.len() != 64
        || !hex
            .bytes()
            .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
    {
        return Err(error(format!("{name} must be a canonical SHA-256 digest")));
    }
    Ok(())
}
fn validate_decimal(name: &str, value: &str) -> Result<u64, ProtocolError> {
    if value.is_empty()
        || (value.len() > 1 && value.starts_with('0'))
        || !value.bytes().all(|byte| byte.is_ascii_digit())
    {
        return Err(error(format!("{name} must be a canonical decimal string")));
    }
    decimal_value(value)
}
fn decimal_value(value: &str) -> Result<u64, ProtocolError> {
    value
        .parse()
        .map_err(|_| error("decimal string exceeds unsigned 64-bit range"))
}
fn validate_sorted<T>(
    values: &[T],
    key: impl Fn(&T) -> &str,
    name: &str,
) -> Result<(), ProtocolError> {
    if values.windows(2).any(|pair| key(&pair[0]) >= key(&pair[1])) {
        return Err(error(format!("{name} must be sorted and unique")));
    }
    Ok(())
}
fn validate_sorted_strings(values: &[String], name: &str) -> Result<(), ProtocolError> {
    if values.windows(2).any(|pair| pair[0] >= pair[1]) {
        return Err(error(format!("{name} must be sorted and unique")));
    }
    for value in values {
        validate_token(name, value)?;
    }
    Ok(())
}
fn error(detail: impl Into<String>) -> ProtocolError {
    ProtocolError::new(detail)
}