lenso-process-protocol 0.1.0

Language-neutral Process Protocol types 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
//! Language-neutral source types for `lenso-process-jsonrpc-http-v1`.

use std::cmp::Ordering;

use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
use serde::{Deserialize, Serialize};
use serde_json::Value;

mod proof;
mod strict_json;

pub use proof::{
    canonicalize_proof_value, child_proof_message, handshake_params_digest, host_proof_message,
};
pub use strict_json::{decode_strict, encode_compact};

/// Exact Process Protocol profile selected by an Adapter.
pub const PROCESS_PROFILE: &str = "lenso-process-jsonrpc-http-v1";
/// Portable JSON value profile used by request payloads.
pub const VALUE_PROFILE: &str = "lenso-json-value-v1";
/// Mandatory child-provides-request interaction profile.
pub const PROVIDE_REQUEST_PROFILE: &str = "provide-request-v1";
/// Control method used once during process startup.
pub const HANDSHAKE_METHOD: &str = "lenso.process.v1.handshake";
/// Data method used for one request interaction.
pub const REQUEST_METHOD: &str = "lenso.process.v1.request";
/// Control method used for idempotent cancellation.
pub const CANCEL_METHOD: &str = "lenso.process.v1.cancel";
/// Control method used for graceful child shutdown.
pub const SHUTDOWN_METHOD: &str = "lenso.process.v1.shutdown";

/// One bounded readiness record written on the dedicated inherited handle.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ReadinessRecord {
    /// Exact Process Protocol profile implemented by both listeners.
    pub protocol: String,
    /// Ephemeral loopback data-listener port.
    pub data_port: u16,
    /// Distinct ephemeral loopback control-listener port.
    pub control_port: u16,
}

impl ReadinessRecord {
    /// Validates the exact profile and two non-zero, distinct ports.
    pub fn validate(&self) -> Result<(), ProtocolError> {
        if self.protocol != PROCESS_PROFILE {
            return Err(ProtocolError::new("unsupported Process Protocol profile"));
        }
        if self.data_port == 0 || self.control_port == 0 || self.data_port == self.control_port {
            return Err(ProtocolError::new(
                "readiness requires distinct non-zero data and control ports",
            ));
        }
        Ok(())
    }
}

/// Error returned when a Process Protocol document is malformed or inconsistent.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ProtocolError {
    detail: String,
}

impl ProtocolError {
    fn new(detail: impl Into<String>) -> Self {
        Self {
            detail: detail.into(),
        }
    }

    /// Returns the stable human-readable validation detail.
    #[must_use]
    pub fn detail(&self) -> &str {
        &self.detail
    }
}

impl std::fmt::Display for ProtocolError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(&self.detail)
    }
}

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

/// Peer-confirmed limits that must match exactly during handshake.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PeerLimits {
    /// Maximum encoded data request or response body.
    pub max_http_body_bytes: u64,
    /// Maximum encoded control request or response body.
    pub max_control_http_body_bytes: u64,
    /// Maximum concurrently active request handlers.
    pub max_concurrent_requests: u32,
    /// Maximum queued child data requests.
    pub child_request_queue_capacity: u32,
    /// Maximum completed correlation IDs retained in one process session.
    pub max_retired_correlation_ids: u32,
    /// Maximum queued control requests, with at least one reserved handler.
    pub control_queue_capacity: u32,
}

impl PeerLimits {
    /// Returns the V1 defaults.
    #[must_use]
    pub const fn v1_defaults() -> Self {
        Self {
            max_http_body_bytes: 65_536,
            max_control_http_body_bytes: 16_384,
            max_concurrent_requests: 32,
            child_request_queue_capacity: 32,
            max_retired_correlation_ids: 65_536,
            control_queue_capacity: 32,
        }
    }

    /// Enforces non-zero V1 hard maxima.
    pub fn validate(&self) -> Result<(), ProtocolError> {
        validate_limit("max_http_body_bytes", self.max_http_body_bytes, 1_048_576)?;
        validate_limit(
            "max_control_http_body_bytes",
            self.max_control_http_body_bytes,
            65_536,
        )?;
        validate_limit(
            "max_concurrent_requests",
            u64::from(self.max_concurrent_requests),
            256,
        )?;
        validate_limit(
            "child_request_queue_capacity",
            u64::from(self.child_request_queue_capacity),
            1_024,
        )?;
        validate_limit(
            "max_retired_correlation_ids",
            u64::from(self.max_retired_correlation_ids),
            1_048_576,
        )?;
        validate_limit(
            "control_queue_capacity",
            u64::from(self.control_queue_capacity),
            256,
        )
    }
}

/// One exact provided operation in handshake identity.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OperationDescriptor {
    /// Stable operation name.
    pub operation: String,
    /// Exact interaction kind. V1 supports request provision only.
    pub interaction: InteractionKind,
}

/// Interaction kinds admitted by the base V1 profile.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InteractionKind {
    /// One request with one terminal outcome.
    Request,
}

/// One exact provided Capability Descriptor.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CapabilityDescriptor {
    /// Stable `namespace.name@major` identity.
    pub capability_id: String,
    /// Exact Descriptor semantic version.
    pub descriptor_version: String,
    /// Canonical Descriptor SHA-256 digest.
    pub descriptor_digest: String,
    /// Canonical-sorted operation table.
    pub operations: Vec<OperationDescriptor>,
}

/// One exact outbound binding exposed only by a selected consume profile.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OutboundBindingDescriptor {
    /// Stable binding identity within the Module Instance.
    pub binding_id: String,
    /// Required Capability identity.
    pub capability_id: String,
    /// Exact Descriptor version.
    pub descriptor_version: String,
    /// Canonical Descriptor digest.
    pub descriptor_digest: String,
    /// Explicit provider Module Instance key.
    pub provider_instance: String,
}

/// Immutable identity the child must echo exactly during handshake.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct HandshakeIdentity {
    /// Exact transport profile.
    pub protocol_profile: String,
    /// Exact portable value profile.
    pub value_profile: String,
    /// App-local Module Instance key.
    pub module_instance: String,
    /// Canonical decimal Module generation.
    pub module_generation: String,
    /// Exact App Generation Spec digest.
    pub generation_spec_digest: String,
    /// Exact executable Artifact digest.
    pub artifact_digest: String,
    /// Exact Effective Host Grant Set digest.
    pub effective_host_grant_set_digest: String,
    /// Canonical-sorted exact interaction profiles.
    pub interaction_profiles: Vec<String>,
    /// Canonical-sorted provided Capability Descriptors.
    pub provided_capabilities: Vec<CapabilityDescriptor>,
    /// Canonical-sorted explicit outbound bindings.
    pub outbound_bindings: Vec<OutboundBindingDescriptor>,
    /// Limits both peers must confirm exactly.
    pub peer_limits: PeerLimits,
}

/// Host-to-child handshake params.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct HandshakeParams {
    /// Exact identity to admit.
    pub identity: HandshakeIdentity,
    /// Canonical base64url encoding of 32 random bytes.
    pub host_nonce: String,
    /// Canonical base64url HMAC output.
    pub host_proof: String,
}

/// Child-to-host accepted handshake result.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct HandshakeResult {
    /// Exact admitted identity echoed from the request.
    pub identity: HandshakeIdentity,
    /// Canonical base64url encoding of 32 random bytes.
    pub session: String,
    /// Canonical base64url HMAC output.
    pub child_proof: String,
}

/// Portable invocation extension transported without domain interpretation.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct InvocationExtension {
    /// Stable unique extension key.
    pub key: String,
    /// Canonical padded Base64 bytes.
    pub value: String,
    /// Optional issuing authority.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub issuer: Option<String>,
    /// Canonical-sorted declared audiences.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub audience: Vec<String>,
    /// Optional opaque proof.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub proof: Option<String>,
    /// Whether the extension value is sealed for its audience.
    #[serde(default, skip_serializing_if = "is_false")]
    pub sealed: bool,
}

/// Host-to-child request params for `provide-request-v1`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RequestParams {
    /// Authenticated process session.
    pub session: String,
    /// Opaque canonical decimal-string correlation ID.
    pub correlation_id: String,
    /// Selected Capability identity.
    pub capability_id: String,
    /// Exact selected Descriptor version.
    pub descriptor_version: String,
    /// Exact selected Descriptor digest.
    pub descriptor_digest: String,
    /// Selected operation.
    pub operation: String,
    /// Exact request interaction marker.
    pub interaction: InteractionKind,
    /// Host-established caller Module Instance, or `null` for host ingress.
    pub caller_instance: Option<String>,
    /// Relative remaining budget as decimal nanoseconds, or `null` for none.
    pub remaining_timeout_nanos: Option<String>,
    /// Structurally validated portable invocation extensions.
    #[serde(default)]
    pub extensions: Vec<InvocationExtension>,
    /// Capability-defined portable JSON payload.
    pub payload: Value,
}

/// Child-to-host request result.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RequestResult {
    /// Authenticated process session.
    pub session: String,
    /// Correlation ID echoed from the request.
    pub correlation_id: String,
    /// Exactly one terminal application outcome.
    pub outcome: ProcessOutcome,
}

/// Child-originated request outcomes allowed by V1.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum ProcessOutcome {
    /// Capability success payload.
    Success { value: Value },
    /// Capability-defined Domain Error payload.
    Domain { error: Value },
    /// Narrow child-originated Runtime Failure.
    Runtime { failure: ChildRuntimeFailure },
}

/// Runtime Failures the child is permitted to originate.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum ChildRuntimeFailure {
    /// The child cannot admit this operation within its declared bound.
    ResourceExhausted { operation: String },
    /// The Module generation is no longer trustworthy and must be retired.
    ModuleFailure { detail: String },
}

/// Idempotent cancel params.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CancelParams {
    /// Authenticated process session.
    pub session: String,
    /// Active, retired, or unknown correlation ID.
    pub correlation_id: String,
}

/// Graceful shutdown params.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ShutdownParams {
    /// Authenticated process session.
    pub session: String,
}

/// Successful cancel or shutdown acknowledgement.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ControlAck {
    /// Authenticated process session.
    pub session: String,
    /// Always true; cancel does not reveal correlation existence.
    pub accepted: bool,
}

/// Strict JSON-RPC request envelope.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct JsonRpcRequest<T> {
    /// Must equal `2.0`.
    pub jsonrpc: String,
    /// Canonical decimal-string JSON-RPC ID.
    pub id: String,
    /// Exact versioned method.
    pub method: String,
    /// Strict method params object.
    pub params: T,
}

/// Strict successful JSON-RPC response envelope.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct JsonRpcSuccess<T> {
    /// Must equal `2.0`.
    pub jsonrpc: String,
    /// Exact request ID.
    pub id: String,
    /// Strict method result object.
    pub result: T,
}

/// Strict JSON-RPC 2.0 error object used before a method result exists.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct JsonRpcErrorObject {
    /// Standard JSON-RPC error code.
    pub code: i32,
    /// Stable public error message without host or child internals.
    pub message: String,
}

/// Strict JSON-RPC 2.0 error response. Parse/invalid-request errors use a null ID.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct JsonRpcError {
    /// Must equal `2.0`.
    pub jsonrpc: String,
    /// Exact request ID, or null when the request ID could not be authenticated.
    pub id: Option<String>,
    /// Standard error object.
    pub error: JsonRpcErrorObject,
}

impl HandshakeIdentity {
    /// Validates exact profiles, digests, canonical ordering, and V1 limits.
    pub fn validate(&self) -> Result<(), ProtocolError> {
        if self.protocol_profile != PROCESS_PROFILE {
            return Err(ProtocolError::new("unsupported Process Protocol profile"));
        }
        if self.value_profile != VALUE_PROFILE {
            return Err(ProtocolError::new("unsupported portable value profile"));
        }
        validate_token("module_instance", &self.module_instance)?;
        validate_decimal("module_generation", &self.module_generation)?;
        validate_digest("generation_spec_digest", &self.generation_spec_digest)?;
        validate_digest("artifact_digest", &self.artifact_digest)?;
        validate_digest(
            "effective_host_grant_set_digest",
            &self.effective_host_grant_set_digest,
        )?;
        validate_sorted_unique("interaction_profiles", &self.interaction_profiles, Ord::cmp)?;
        if self.interaction_profiles.as_slice() != [PROVIDE_REQUEST_PROFILE] {
            return Err(ProtocolError::new(
                "V1 requires exactly the provide-request-v1 interaction profile",
            ));
        }
        validate_sorted_unique(
            "provided_capabilities",
            &self.provided_capabilities,
            |left, right| left.capability_id.cmp(&right.capability_id),
        )?;
        if self.provided_capabilities.is_empty() {
            return Err(ProtocolError::new(
                "at least one provided Capability is required",
            ));
        }
        for descriptor in &self.provided_capabilities {
            descriptor.validate()?;
        }
        if !self.outbound_bindings.is_empty() {
            return Err(ProtocolError::new(
                "outbound bindings require a selected consume profile",
            ));
        }
        self.peer_limits.validate()
    }
}

impl CapabilityDescriptor {
    fn validate(&self) -> Result<(), ProtocolError> {
        validate_token("capability_id", &self.capability_id)?;
        validate_token("descriptor_version", &self.descriptor_version)?;
        validate_digest("descriptor_digest", &self.descriptor_digest)?;
        validate_sorted_unique("operations", &self.operations, |left, right| {
            left.operation.cmp(&right.operation)
        })?;
        if self.operations.is_empty() {
            return Err(ProtocolError::new(
                "provided Capability operations cannot be empty",
            ));
        }
        for operation in &self.operations {
            validate_token("operation", &operation.operation)?;
        }
        Ok(())
    }
}

impl HandshakeParams {
    /// Validates identity, nonce, and proof encoding.
    pub fn validate(&self) -> Result<(), ProtocolError> {
        self.identity.validate()?;
        decode_base64url_32("host_nonce", &self.host_nonce)?;
        decode_base64url_32("host_proof", &self.host_proof)?;
        Ok(())
    }
}

impl HandshakeResult {
    /// Validates the child echo and proof encoding against the expected identity.
    pub fn validate_against(&self, expected: &HandshakeIdentity) -> Result<(), ProtocolError> {
        self.identity.validate()?;
        if &self.identity != expected {
            return Err(ProtocolError::new("child handshake identity mismatch"));
        }
        decode_base64url_32("session", &self.session)?;
        decode_base64url_32("child_proof", &self.child_proof)?;
        Ok(())
    }
}

impl RequestParams {
    /// Validates host-owned request envelope fields before payload Schema decoding.
    pub fn validate(&self) -> Result<(), ProtocolError> {
        decode_base64url_32("session", &self.session)?;
        validate_decimal("correlation_id", &self.correlation_id)?;
        validate_token("capability_id", &self.capability_id)?;
        validate_token("descriptor_version", &self.descriptor_version)?;
        validate_digest("descriptor_digest", &self.descriptor_digest)?;
        validate_token("operation", &self.operation)?;
        if let Some(caller) = &self.caller_instance {
            validate_token("caller_instance", caller)?;
        }
        if let Some(timeout) = &self.remaining_timeout_nanos {
            validate_decimal("remaining_timeout_nanos", timeout)?;
        }
        validate_sorted_unique("extensions", &self.extensions, |left, right| {
            left.key.cmp(&right.key)
        })?;
        for extension in &self.extensions {
            extension.validate()?;
        }
        Ok(())
    }
}

impl InvocationExtension {
    fn validate(&self) -> Result<(), ProtocolError> {
        validate_token("extension key", &self.key)?;
        let decoded = base64::engine::general_purpose::STANDARD
            .decode(&self.value)
            .map_err(|_| ProtocolError::new("extension value must be padded Base64"))?;
        if base64::engine::general_purpose::STANDARD.encode(decoded) != self.value {
            return Err(ProtocolError::new(
                "extension value must use canonical padded Base64",
            ));
        }
        validate_sorted_unique("extension audience", &self.audience, |left, right| {
            left.cmp(right)
        })?;
        Ok(())
    }
}

impl RequestResult {
    /// Validates session, correlation, and bounded child Runtime Failure fields.
    pub fn validate(&self) -> Result<(), ProtocolError> {
        decode_base64url_32("session", &self.session)?;
        validate_decimal("correlation_id", &self.correlation_id)?;
        if let ProcessOutcome::Runtime { failure } = &self.outcome {
            match failure {
                ChildRuntimeFailure::ResourceExhausted { operation } => {
                    validate_token("operation", operation)?;
                }
                ChildRuntimeFailure::ModuleFailure { detail } => {
                    if detail.is_empty() || detail.len() > 1_024 {
                        return Err(ProtocolError::new(
                            "Module Failure detail must contain 1..=1024 bytes",
                        ));
                    }
                }
            }
        }
        Ok(())
    }
}

impl CancelParams {
    /// Validates the authenticated cancel identity.
    pub fn validate(&self) -> Result<(), ProtocolError> {
        decode_base64url_32("session", &self.session)?;
        validate_decimal("correlation_id", &self.correlation_id).map(|_| ())
    }
}

impl ShutdownParams {
    /// Validates the authenticated shutdown identity.
    pub fn validate(&self) -> Result<(), ProtocolError> {
        decode_base64url_32("session", &self.session)?;
        Ok(())
    }
}

impl ControlAck {
    /// Validates an authenticated, affirmative control acknowledgement.
    pub fn validate(&self) -> Result<(), ProtocolError> {
        decode_base64url_32("session", &self.session)?;
        if !self.accepted {
            return Err(ProtocolError::new(
                "control acknowledgement must be accepted",
            ));
        }
        Ok(())
    }
}

impl<T> JsonRpcRequest<T> {
    /// Validates the base JSON-RPC envelope and exact expected method.
    pub fn validate_envelope(&self, expected_method: &str) -> Result<(), ProtocolError> {
        if self.jsonrpc != "2.0" {
            return Err(ProtocolError::new("jsonrpc must equal 2.0"));
        }
        validate_decimal("JSON-RPC id", &self.id)?;
        if self.method != expected_method {
            return Err(ProtocolError::new("unexpected JSON-RPC method"));
        }
        Ok(())
    }
}

impl<T> JsonRpcSuccess<T> {
    /// Validates the base JSON-RPC success envelope and expected request ID.
    pub fn validate_envelope(&self, expected_id: &str) -> Result<(), ProtocolError> {
        if self.jsonrpc != "2.0" {
            return Err(ProtocolError::new("jsonrpc must equal 2.0"));
        }
        validate_decimal("JSON-RPC id", &self.id)?;
        if self.id != expected_id {
            return Err(ProtocolError::new("JSON-RPC response id mismatch"));
        }
        Ok(())
    }
}

impl JsonRpcError {
    /// Validates the closed V1 JSON-RPC error surface and optional request ID.
    pub fn validate(&self) -> Result<(), ProtocolError> {
        if self.jsonrpc != "2.0" {
            return Err(ProtocolError::new("jsonrpc must equal 2.0"));
        }
        if let Some(id) = &self.id {
            validate_decimal("JSON-RPC id", id)?;
        }
        if !matches!(self.error.code, -32700 | -32600 | -32601 | -32602 | -32603) {
            return Err(ProtocolError::new("unsupported JSON-RPC error code"));
        }
        if self.error.message.is_empty() || self.error.message.len() > 256 {
            return Err(ProtocolError::new(
                "JSON-RPC error message must contain 1..=256 bytes",
            ));
        }
        Ok(())
    }
}

fn validate_limit(name: &str, value: u64, maximum: u64) -> Result<(), ProtocolError> {
    if value == 0 || value > maximum {
        return Err(ProtocolError::new(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(ProtocolError::new(format!(
            "{name} must be a 1..=256 byte portable token"
        )));
    }
    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(ProtocolError::new(format!(
            "{name} must be a canonical non-negative decimal string"
        )));
    }
    value.parse::<u64>().map_err(|_| {
        ProtocolError::new(format!(
            "{name} exceeds the unsigned 64-bit decimal profile"
        ))
    })
}

fn validate_digest(name: &str, value: &str) -> Result<(), ProtocolError> {
    let Some(hex) = value.strip_prefix("sha256:") else {
        return Err(ProtocolError::new(format!(
            "{name} must use a sha256: prefix"
        )));
    };
    if hex.len() != 64
        || !hex
            .bytes()
            .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
    {
        return Err(ProtocolError::new(format!(
            "{name} must contain 64 lowercase hexadecimal characters"
        )));
    }
    Ok(())
}

fn decode_base64url_32(name: &str, value: &str) -> Result<[u8; 32], ProtocolError> {
    let decoded = URL_SAFE_NO_PAD
        .decode(value)
        .map_err(|_| ProtocolError::new(format!("{name} must be canonical base64url")))?;
    let decoded: [u8; 32] = decoded
        .try_into()
        .map_err(|_| ProtocolError::new(format!("{name} must encode exactly 32 bytes")))?;
    if URL_SAFE_NO_PAD.encode(decoded) != value {
        return Err(ProtocolError::new(format!(
            "{name} must be canonical unpadded base64url"
        )));
    }
    Ok(decoded)
}

fn validate_sorted_unique<T>(
    name: &str,
    values: &[T],
    compare: impl Fn(&T, &T) -> Ordering,
) -> Result<(), ProtocolError> {
    if values
        .windows(2)
        .any(|pair| compare(&pair[0], &pair[1]) != Ordering::Less)
    {
        return Err(ProtocolError::new(format!(
            "{name} must be strictly canonical-sorted and unique"
        )));
    }
    Ok(())
}

#[allow(clippy::trivially_copy_pass_by_ref)]
const fn is_false(value: &bool) -> bool {
    !*value
}