stateknot-core 0.1.0-alpha.1

Core domain types for the StateKnot agent runtime
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
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
// Copyright 2026 StateKnot contributors
// SPDX-License-Identifier: Apache-2.0

//! Provider- and protocol-neutral tool descriptor contracts.

use std::fmt;

use schemars::JsonSchema;
use serde::{Deserialize, Deserializer, Serialize, de};
use thiserror::Error;

use crate::{
    ByteCount, CapabilityKind, CapabilityMetadata, DurationMillis, ExecutionCount, SchemaReference,
};

/// External side-effect class declared by a trusted tool registry.
///
/// This value is a reviewed semantic claim, not a fact inferred from an HTTP
/// method, provider annotation, or model output. Retry remains subject to the
/// full execution contract, failure advice, deadline, budget, and policy.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ToolRisk {
    /// The tool does not intentionally change externally observable state.
    ReadOnly,
    /// Repeating the same logical write is safe under the declared mechanism.
    IdempotentWrite,
    /// Repeating the write may create an additional external effect.
    NonIdempotentWrite,
}

/// Mechanism governing repeated invocation of a tool operation.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ToolIdempotency {
    /// Idempotency is irrelevant because the tool is read-only.
    NotApplicable,
    /// Identical canonical arguments are intrinsically safe to repeat.
    Intrinsic,
    /// The runtime must provide the same durable idempotency key on every attempt.
    RequiredKey,
    /// The tool cannot guarantee that a repeated write has one business effect.
    Unsupported,
}

/// Validated side-effect and recovery semantics for one tool version.
///
/// Status-query support means an implementation can reconcile an ambiguous
/// invocation outcome. Compensation support means a separately authorized
/// recovery operation exists; it does not make the original write atomic or
/// erase its audit history.
#[derive(Clone, Debug, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ToolExecutionSemantics {
    risk: ToolRisk,
    idempotency: ToolIdempotency,
    status_query: bool,
    compensation: bool,
}

impl ToolExecutionSemantics {
    /// Constructs a cross-field validated execution contract.
    ///
    /// # Errors
    ///
    /// Returns [`ToolExecutionSemanticsError`] when risk and idempotency
    /// conflict, or when a read-only tool claims write-recovery operations.
    pub const fn new(
        risk: ToolRisk,
        idempotency: ToolIdempotency,
        status_query: bool,
        compensation: bool,
    ) -> Result<Self, ToolExecutionSemanticsError> {
        let idempotency_is_valid = matches!(
            (risk, idempotency),
            (ToolRisk::ReadOnly, ToolIdempotency::NotApplicable)
                | (
                    ToolRisk::IdempotentWrite,
                    ToolIdempotency::Intrinsic | ToolIdempotency::RequiredKey
                )
                | (ToolRisk::NonIdempotentWrite, ToolIdempotency::Unsupported)
        );
        if !idempotency_is_valid {
            return Err(ToolExecutionSemanticsError::InvalidIdempotency { risk, idempotency });
        }
        if matches!(risk, ToolRisk::ReadOnly) && (status_query || compensation) {
            return Err(ToolExecutionSemanticsError::ReadOnlyRecoverySupport {
                status_query,
                compensation,
            });
        }
        Ok(Self {
            risk,
            idempotency,
            status_query,
            compensation,
        })
    }

    /// Returns the reviewed external side-effect class.
    #[must_use]
    pub const fn risk(&self) -> ToolRisk {
        self.risk
    }

    /// Returns the declared repeated-invocation mechanism.
    #[must_use]
    pub const fn idempotency(&self) -> ToolIdempotency {
        self.idempotency
    }

    /// Returns whether ambiguous outcomes can be queried authoritatively.
    #[must_use]
    pub const fn supports_status_query(&self) -> bool {
        self.status_query
    }

    /// Returns whether a separately authorized compensation operation exists.
    #[must_use]
    pub const fn supports_compensation(&self) -> bool {
        self.compensation
    }

    /// Returns whether the runtime must supply a durable idempotency key.
    #[must_use]
    pub const fn requires_idempotency_key(&self) -> bool {
        matches!(self.idempotency, ToolIdempotency::RequiredKey)
    }
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct ToolExecutionSemanticsWire {
    risk: ToolRisk,
    idempotency: ToolIdempotency,
    status_query: bool,
    compensation: bool,
}

impl<'de> Deserialize<'de> for ToolExecutionSemantics {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let wire = ToolExecutionSemanticsWire::deserialize(deserializer)?;
        Self::new(
            wire.risk,
            wire.idempotency,
            wire.status_query,
            wire.compensation,
        )
        .map_err(de::Error::custom)
    }
}

/// Invalid tool side-effect and recovery semantics.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum ToolExecutionSemanticsError {
    /// The idempotency mechanism does not match the declared risk class.
    #[error("tool risk {risk:?} is incompatible with idempotency {idempotency:?}")]
    InvalidIdempotency {
        /// Declared side-effect class.
        risk: ToolRisk,
        /// Conflicting repeated-invocation mechanism.
        idempotency: ToolIdempotency,
    },

    /// A read-only tool declared write-recovery behavior.
    #[error(
        "read-only tool cannot declare status-query={status_query} or compensation={compensation}"
    )]
    ReadOnlyRecoverySupport {
        /// Invalid status-query declaration.
        status_query: bool,
        /// Invalid compensation declaration.
        compensation: bool,
    },
}

/// Broad resource access required by a tool implementation.
///
/// Exact destinations, paths, operations, and credential handles live in a
/// tenant-controlled executor profile. This declaration can only cause policy
/// to require or deny access; it never grants access by itself.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ToolResourceAccess {
    /// No access to the resource category is required.
    None,
    /// The tool only reads from the resource category.
    ReadOnly,
    /// The tool may read and write the resource category.
    ReadWrite,
}

/// Resource categories that must be satisfied by an executor profile.
///
/// `dynamic_code` means the tool executes code supplied at invocation time. It
/// does not describe ordinary compiled tool implementation code. `StateKnot` v1
/// does not provide a built-in arbitrary-code sandbox; policy may route such a
/// tool to an independently operated sandbox service or deny it.
#[derive(Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ToolResourceRequirements {
    network: ToolResourceAccess,
    filesystem: ToolResourceAccess,
    credentials: bool,
    dynamic_code: bool,
}

impl ToolResourceRequirements {
    /// Constructs explicit resource-category requirements.
    #[must_use]
    pub const fn new(
        network: ToolResourceAccess,
        filesystem: ToolResourceAccess,
        credentials: bool,
        dynamic_code: bool,
    ) -> Self {
        Self {
            network,
            filesystem,
            credentials,
            dynamic_code,
        }
    }

    /// Constructs a tool requiring no external resource category.
    #[must_use]
    pub const fn none() -> Self {
        Self::new(
            ToolResourceAccess::None,
            ToolResourceAccess::None,
            false,
            false,
        )
    }

    /// Returns the broad network access declaration.
    #[must_use]
    pub const fn network(&self) -> ToolResourceAccess {
        self.network
    }

    /// Returns the broad filesystem access declaration.
    #[must_use]
    pub const fn filesystem(&self) -> ToolResourceAccess {
        self.filesystem
    }

    /// Returns whether opaque credentials must be resolved for an invocation.
    #[must_use]
    pub const fn requires_credentials(&self) -> bool {
        self.credentials
    }

    /// Returns whether invocation-supplied code is executed.
    #[must_use]
    pub const fn executes_dynamic_code(&self) -> bool {
        self.dynamic_code
    }

    /// Returns whether a declared resource category may be written.
    #[must_use]
    pub const fn has_write_access(&self) -> bool {
        matches!(self.network, ToolResourceAccess::ReadWrite)
            || matches!(self.filesystem, ToolResourceAccess::ReadWrite)
    }
}

/// Cancellation behavior exposed by a tool implementation.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ToolCancellationSupport {
    /// The implementation has no cancellation entry point.
    Unsupported,
    /// The implementation observes cancellation on a best-effort basis.
    Cooperative,
}

/// Runtime-facing optional behavior supported by a tool implementation.
///
/// Cooperative cancellation never proves that an external effect did not
/// occur. Progress events are observational and cannot commit tool success.
#[derive(Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ToolInvocationCapabilities {
    cancellation: ToolCancellationSupport,
    max_progress_events: ExecutionCount,
}

impl ToolInvocationCapabilities {
    /// Constructs invocation behavior flags.
    #[must_use]
    pub const fn new(
        cancellation: ToolCancellationSupport,
        max_progress_events: ExecutionCount,
    ) -> Self {
        Self {
            cancellation,
            max_progress_events,
        }
    }

    /// Returns the supported cancellation mode.
    #[must_use]
    pub const fn cancellation(&self) -> ToolCancellationSupport {
        self.cancellation
    }

    /// Returns whether bounded progress events may be emitted.
    #[must_use]
    pub const fn supports_progress_events(&self) -> bool {
        self.max_progress_events.get() != 0
    }

    /// Returns the maximum progress events emitted by one invocation.
    #[must_use]
    pub const fn max_progress_events(&self) -> ExecutionCount {
        self.max_progress_events
    }
}

/// Finite per-invocation execution ceilings declared by a tool version.
///
/// System, tenant, policy, run, and descriptor limits are intersected; these
/// values can never widen another layer. `max_total_artifact_bytes` covers all
/// artifacts produced by one invocation, not one artifact or a complete run.
#[derive(Clone, Debug, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ToolExecutionLimits {
    timeout: DurationMillis,
    max_concurrency: ExecutionCount,
    max_input_bytes: ByteCount,
    max_inline_result_bytes: ByteCount,
    max_artifacts: ExecutionCount,
    max_total_artifact_bytes: ByteCount,
}

impl ToolExecutionLimits {
    /// Constructs finite limits and validates unusable zero combinations.
    ///
    /// # Errors
    ///
    /// Returns [`ToolExecutionLimitsError`] when timeout, concurrency, or
    /// input/inline result capacity is zero, or when artifact count and byte
    /// capacity disagree about whether artifacts are permitted.
    pub const fn new(
        timeout: DurationMillis,
        max_concurrency: ExecutionCount,
        max_input_bytes: ByteCount,
        max_inline_result_bytes: ByteCount,
        max_artifacts: ExecutionCount,
        max_total_artifact_bytes: ByteCount,
    ) -> Result<Self, ToolExecutionLimitsError> {
        if timeout.as_i64() == 0 {
            return Err(ToolExecutionLimitsError::ZeroTimeout);
        }
        if max_concurrency.get() == 0 {
            return Err(ToolExecutionLimitsError::ZeroConcurrency);
        }
        if max_input_bytes.get() == 0 {
            return Err(ToolExecutionLimitsError::ZeroInputBytes);
        }
        if max_inline_result_bytes.get() == 0 {
            return Err(ToolExecutionLimitsError::ZeroInlineResultBytes);
        }
        if (max_artifacts.get() == 0) != (max_total_artifact_bytes.get() == 0) {
            return Err(ToolExecutionLimitsError::ArtifactCapacityMismatch {
                max_artifacts,
                max_total_artifact_bytes,
            });
        }
        Ok(Self {
            timeout,
            max_concurrency,
            max_input_bytes,
            max_inline_result_bytes,
            max_artifacts,
            max_total_artifact_bytes,
        })
    }

    /// Returns the maximum elapsed time for one invocation attempt.
    #[must_use]
    pub const fn timeout(&self) -> DurationMillis {
        self.timeout
    }

    /// Returns the maximum concurrent calls for this capability version.
    #[must_use]
    pub const fn max_concurrency(&self) -> ExecutionCount {
        self.max_concurrency
    }

    /// Returns the maximum compact encoded argument bytes for one invocation.
    #[must_use]
    pub const fn max_input_bytes(&self) -> ByteCount {
        self.max_input_bytes
    }

    /// Returns the maximum inline result bytes from one invocation.
    #[must_use]
    pub const fn max_inline_result_bytes(&self) -> ByteCount {
        self.max_inline_result_bytes
    }

    /// Returns the maximum number of artifacts from one invocation.
    #[must_use]
    pub const fn max_artifacts(&self) -> ExecutionCount {
        self.max_artifacts
    }

    /// Returns the maximum total artifact bytes from one invocation.
    #[must_use]
    pub const fn max_total_artifact_bytes(&self) -> ByteCount {
        self.max_total_artifact_bytes
    }
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct ToolExecutionLimitsWire {
    timeout: DurationMillis,
    max_concurrency: ExecutionCount,
    max_input_bytes: ByteCount,
    max_inline_result_bytes: ByteCount,
    max_artifacts: ExecutionCount,
    max_total_artifact_bytes: ByteCount,
}

impl<'de> Deserialize<'de> for ToolExecutionLimits {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let wire = ToolExecutionLimitsWire::deserialize(deserializer)?;
        Self::new(
            wire.timeout,
            wire.max_concurrency,
            wire.max_input_bytes,
            wire.max_inline_result_bytes,
            wire.max_artifacts,
            wire.max_total_artifact_bytes,
        )
        .map_err(de::Error::custom)
    }
}

/// Invalid per-invocation tool limits.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum ToolExecutionLimitsError {
    /// A zero timeout would make every invocation ineligible.
    #[error("tool timeout must be greater than zero milliseconds")]
    ZeroTimeout,

    /// A zero concurrency ceiling would make every invocation ineligible.
    #[error("tool max concurrency must be greater than zero")]
    ZeroConcurrency,

    /// Schema validation and invocation require a positive argument capacity.
    #[error("tool max input bytes must be greater than zero")]
    ZeroInputBytes,

    /// A successful inline result needs a positive encoded byte capacity.
    #[error("tool max inline result bytes must be greater than zero")]
    ZeroInlineResultBytes,

    /// Artifact count and total bytes disagreed about artifact support.
    #[error(
        "tool artifact capacity requires both count and bytes to be zero or both to be positive"
    )]
    ArtifactCapacityMismatch {
        /// Declared maximum artifact count.
        max_artifacts: ExecutionCount,
        /// Declared maximum total artifact bytes.
        max_total_artifact_bytes: ByteCount,
    },
}

/// Immutable, protocol-neutral description of one executable tool version.
///
/// Schema references are identities only. Before registration, a trusted local
/// schema registry verifies their digests, JSON Schema 2020-12 documents, and
/// provider compatibility profile. Before selection, the tenant registry
/// authenticates metadata ownership, applies policy, and snapshots this exact
/// descriptor for the invocation attempt.
#[derive(Clone, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ToolDescriptor {
    metadata: CapabilityMetadata,
    input_schema: SchemaReference,
    output_schema: SchemaReference,
    semantics: ToolExecutionSemantics,
    resources: ToolResourceRequirements,
    invocation: ToolInvocationCapabilities,
    limits: ToolExecutionLimits,
}

impl ToolDescriptor {
    /// Constructs a descriptor and validates cross-component invariants.
    ///
    /// # Errors
    ///
    /// Returns [`ToolDescriptorError`] when common metadata is not a tool or a
    /// read-only tool declares network/filesystem write access.
    pub fn new(
        metadata: CapabilityMetadata,
        input_schema: SchemaReference,
        output_schema: SchemaReference,
        semantics: ToolExecutionSemantics,
        resources: ToolResourceRequirements,
        invocation: ToolInvocationCapabilities,
        limits: ToolExecutionLimits,
    ) -> Result<Self, ToolDescriptorError> {
        if metadata.kind() != CapabilityKind::Tool {
            return Err(ToolDescriptorError::WrongCapabilityKind {
                actual: metadata.kind(),
            });
        }
        if semantics.risk() == ToolRisk::ReadOnly && resources.has_write_access() {
            return Err(ToolDescriptorError::ReadOnlyResourceWrite {
                network: resources.network(),
                filesystem: resources.filesystem(),
            });
        }
        Ok(Self {
            metadata,
            input_schema,
            output_schema,
            semantics,
            resources,
            invocation,
            limits,
        })
    }

    /// Returns common identity, discovery, lifecycle, scope, and extension data.
    #[must_use]
    pub const fn metadata(&self) -> &CapabilityMetadata {
        &self.metadata
    }

    /// Returns the immutable input schema identity.
    #[must_use]
    pub const fn input_schema(&self) -> &SchemaReference {
        &self.input_schema
    }

    /// Returns the immutable output schema identity.
    #[must_use]
    pub const fn output_schema(&self) -> &SchemaReference {
        &self.output_schema
    }

    /// Returns side-effect, idempotency, and recovery semantics.
    #[must_use]
    pub const fn semantics(&self) -> &ToolExecutionSemantics {
        &self.semantics
    }

    /// Returns resource categories that an executor profile must satisfy.
    #[must_use]
    pub const fn resources(&self) -> &ToolResourceRequirements {
        &self.resources
    }

    /// Returns optional invocation behavior implemented by this tool.
    #[must_use]
    pub const fn invocation(&self) -> &ToolInvocationCapabilities {
        &self.invocation
    }

    /// Returns finite per-invocation ceilings.
    #[must_use]
    pub const fn limits(&self) -> &ToolExecutionLimits {
        &self.limits
    }
}

impl fmt::Debug for ToolDescriptor {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("ToolDescriptor")
            .field("metadata", &self.metadata)
            .field("input_schema", &self.input_schema)
            .field("output_schema", &self.output_schema)
            .field("semantics", &self.semantics)
            .field("resources", &self.resources)
            .field("invocation", &self.invocation)
            .field("limits", &self.limits)
            .finish_non_exhaustive()
    }
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct ToolDescriptorWire {
    metadata: CapabilityMetadata,
    input_schema: SchemaReference,
    output_schema: SchemaReference,
    semantics: ToolExecutionSemantics,
    resources: ToolResourceRequirements,
    invocation: ToolInvocationCapabilities,
    limits: ToolExecutionLimits,
}

impl<'de> Deserialize<'de> for ToolDescriptor {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let wire = ToolDescriptorWire::deserialize(deserializer)?;
        Self::new(
            wire.metadata,
            wire.input_schema,
            wire.output_schema,
            wire.semantics,
            wire.resources,
            wire.invocation,
            wire.limits,
        )
        .map_err(de::Error::custom)
    }
}

/// Invalid cross-component tool descriptor.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum ToolDescriptorError {
    /// Common metadata classified the capability as something other than a tool.
    #[error("tool descriptor requires kind=tool, received {actual:?}")]
    WrongCapabilityKind {
        /// Conflicting capability kind.
        actual: CapabilityKind,
    },

    /// A read-only tool requested write access to a broad resource category.
    #[error(
        "read-only tool cannot declare network={network:?} or filesystem={filesystem:?} write access"
    )]
    ReadOnlyResourceWrite {
        /// Conflicting network access.
        network: ToolResourceAccess,
        /// Conflicting filesystem access.
        filesystem: ToolResourceAccess,
    },
}

#[cfg(test)]
mod tests {
    use super::*;
    use proptest::prelude::*;
    use serde_json::{Value, from_value, json, to_value};

    use crate::{
        BoundedJson, CapabilityDescription, CapabilityIdentity, CapabilityLifecycle,
        CapabilityName, CapabilityReference, CapabilityTitle, Digest, ExtensionKey, ExtensionValue,
        Extensions, IssuerId, PrincipalIdentity, SchemaId, ScopeSet, SubjectId, Version,
    };

    fn principal() -> PrincipalIdentity {
        PrincipalIdentity::new(
            "https://issuer.example.com/tenant"
                .parse::<IssuerId>()
                .unwrap(),
            "registry-owner".parse::<SubjectId>().unwrap(),
        )
    }

    fn metadata(kind: CapabilityKind, secret: &str) -> CapabilityMetadata {
        let identity = CapabilityIdentity::new(
            principal(),
            CapabilityReference::new(
                "payments.capture".parse::<CapabilityName>().unwrap(),
                Version::new(2, 1, 0),
            ),
        );
        let extensions = Extensions::try_new([(
            ExtensionKey::new("com.example.tool").unwrap(),
            ExtensionValue::opaque(
                BoundedJson::try_from_value(json!({ "secret": secret })).unwrap(),
            ),
        )])
        .unwrap();
        CapabilityMetadata::new(
            identity,
            kind,
            Some(CapabilityTitle::new(format!("Capture payment {secret}")).unwrap()),
            CapabilityDescription::new(format!("Capture one approved payment. {secret}")).unwrap(),
            CapabilityLifecycle::active(),
            ScopeSet::empty(),
            extensions,
        )
        .unwrap()
    }

    fn schema(name: &str) -> SchemaReference {
        SchemaReference::new(
            format!("https://schemas.example.com/tools/{name}/1.0.0")
                .parse::<SchemaId>()
                .unwrap(),
            Version::new(1, 0, 0),
            Digest::sha256(name),
        )
    }

    fn read_only_semantics() -> ToolExecutionSemantics {
        ToolExecutionSemantics::new(
            ToolRisk::ReadOnly,
            ToolIdempotency::NotApplicable,
            false,
            false,
        )
        .unwrap()
    }

    fn default_limits() -> ToolExecutionLimits {
        ToolExecutionLimits::new(
            DurationMillis::new(30_000).unwrap(),
            ExecutionCount::new(16),
            ByteCount::new(64 * 1024),
            ByteCount::new(256 * 1024),
            ExecutionCount::new(4),
            ByteCount::new(25 * 1024 * 1024),
        )
        .unwrap()
    }

    fn descriptor(secret: &str) -> ToolDescriptor {
        ToolDescriptor::new(
            metadata(CapabilityKind::Tool, secret),
            schema("capture-input"),
            schema("capture-output"),
            ToolExecutionSemantics::new(
                ToolRisk::IdempotentWrite,
                ToolIdempotency::RequiredKey,
                true,
                true,
            )
            .unwrap(),
            ToolResourceRequirements::new(
                ToolResourceAccess::ReadWrite,
                ToolResourceAccess::None,
                true,
                false,
            ),
            ToolInvocationCapabilities::new(
                ToolCancellationSupport::Cooperative,
                ExecutionCount::new(128),
            ),
            default_limits(),
        )
        .unwrap()
    }

    #[test]
    fn tool_enums_have_closed_canonical_wire_values() {
        for (value, expected) in [
            (ToolRisk::ReadOnly, "read_only"),
            (ToolRisk::IdempotentWrite, "idempotent_write"),
            (ToolRisk::NonIdempotentWrite, "non_idempotent_write"),
        ] {
            assert_eq!(to_value(value).unwrap(), Value::from(expected));
            assert_eq!(from_value::<ToolRisk>(json!(expected)).unwrap(), value);
        }
        for (value, expected) in [
            (ToolIdempotency::NotApplicable, "not_applicable"),
            (ToolIdempotency::Intrinsic, "intrinsic"),
            (ToolIdempotency::RequiredKey, "required_key"),
            (ToolIdempotency::Unsupported, "unsupported"),
        ] {
            assert_eq!(to_value(value).unwrap(), Value::from(expected));
            assert_eq!(
                from_value::<ToolIdempotency>(json!(expected)).unwrap(),
                value
            );
        }
        for (value, expected) in [
            (ToolResourceAccess::None, "none"),
            (ToolResourceAccess::ReadOnly, "read_only"),
            (ToolResourceAccess::ReadWrite, "read_write"),
        ] {
            assert_eq!(to_value(value).unwrap(), Value::from(expected));
            assert_eq!(
                from_value::<ToolResourceAccess>(json!(expected)).unwrap(),
                value
            );
        }
        for (value, expected) in [
            (ToolCancellationSupport::Unsupported, "unsupported"),
            (ToolCancellationSupport::Cooperative, "cooperative"),
        ] {
            assert_eq!(to_value(value).unwrap(), Value::from(expected));
            assert_eq!(
                from_value::<ToolCancellationSupport>(json!(expected)).unwrap(),
                value
            );
        }
        assert!(from_value::<ToolRisk>(json!("unknown")).is_err());
        assert!(from_value::<ToolIdempotency>(Value::Null).is_err());
        assert!(from_value::<ToolResourceAccess>(json!(42)).is_err());
    }

    #[test]
    fn execution_semantics_reject_every_incoherent_pair() {
        let risks = [
            ToolRisk::ReadOnly,
            ToolRisk::IdempotentWrite,
            ToolRisk::NonIdempotentWrite,
        ];
        let idempotency_modes = [
            ToolIdempotency::NotApplicable,
            ToolIdempotency::Intrinsic,
            ToolIdempotency::RequiredKey,
            ToolIdempotency::Unsupported,
        ];

        for risk in risks {
            for idempotency in idempotency_modes {
                let expected_valid = matches!(
                    (risk, idempotency),
                    (ToolRisk::ReadOnly, ToolIdempotency::NotApplicable)
                        | (
                            ToolRisk::IdempotentWrite,
                            ToolIdempotency::Intrinsic | ToolIdempotency::RequiredKey
                        )
                        | (ToolRisk::NonIdempotentWrite, ToolIdempotency::Unsupported)
                );
                assert_eq!(
                    ToolExecutionSemantics::new(risk, idempotency, false, false).is_ok(),
                    expected_valid,
                    "unexpected result for {risk:?}/{idempotency:?}"
                );
            }
        }

        for (status_query, compensation) in [(true, false), (false, true), (true, true)] {
            assert_eq!(
                ToolExecutionSemantics::new(
                    ToolRisk::ReadOnly,
                    ToolIdempotency::NotApplicable,
                    status_query,
                    compensation,
                ),
                Err(ToolExecutionSemanticsError::ReadOnlyRecoverySupport {
                    status_query,
                    compensation,
                })
            );
        }
    }

    #[test]
    fn execution_semantics_round_trip_and_revalidate_wire_data() {
        let semantics = ToolExecutionSemantics::new(
            ToolRisk::IdempotentWrite,
            ToolIdempotency::RequiredKey,
            true,
            true,
        )
        .unwrap();
        assert_eq!(semantics.risk(), ToolRisk::IdempotentWrite);
        assert_eq!(semantics.idempotency(), ToolIdempotency::RequiredKey);
        assert!(semantics.requires_idempotency_key());
        assert!(semantics.supports_status_query());
        assert!(semantics.supports_compensation());

        let expected = json!({
            "risk": "idempotent_write",
            "idempotency": "required_key",
            "status_query": true,
            "compensation": true
        });
        assert_eq!(to_value(&semantics).unwrap(), expected);
        assert_eq!(
            from_value::<ToolExecutionSemantics>(expected).unwrap(),
            semantics
        );

        for invalid in [
            json!({
                "risk": "idempotent_write",
                "idempotency": "unsupported",
                "status_query": false,
                "compensation": false
            }),
            json!({
                "risk": "read_only",
                "idempotency": "not_applicable",
                "status_query": true,
                "compensation": false
            }),
            json!({
                "risk": "read_only",
                "idempotency": "not_applicable",
                "status_query": false,
                "compensation": false,
                "safe": true
            }),
            Value::Null,
        ] {
            assert!(
                from_value::<ToolExecutionSemantics>(invalid.clone()).is_err(),
                "accepted semantics {invalid}"
            );
        }
    }

    #[test]
    fn resource_and_invocation_capabilities_are_explicit_closed_claims() {
        let none = ToolResourceRequirements::none();
        assert_eq!(none.network(), ToolResourceAccess::None);
        assert_eq!(none.filesystem(), ToolResourceAccess::None);
        assert!(!none.requires_credentials());
        assert!(!none.executes_dynamic_code());
        assert!(!none.has_write_access());

        let resources = ToolResourceRequirements::new(
            ToolResourceAccess::ReadOnly,
            ToolResourceAccess::ReadWrite,
            true,
            true,
        );
        assert!(resources.has_write_access());
        assert!(resources.requires_credentials());
        assert!(resources.executes_dynamic_code());
        let encoded = to_value(&resources).unwrap();
        assert_eq!(
            from_value::<ToolResourceRequirements>(encoded.clone()).unwrap(),
            resources
        );
        let mut unknown = encoded;
        unknown["shell"] = json!(true);
        assert!(from_value::<ToolResourceRequirements>(unknown).is_err());

        let invocation = ToolInvocationCapabilities::new(
            ToolCancellationSupport::Cooperative,
            ExecutionCount::new(128),
        );
        assert_eq!(
            invocation.cancellation(),
            ToolCancellationSupport::Cooperative
        );
        assert!(invocation.supports_progress_events());
        assert_eq!(invocation.max_progress_events(), ExecutionCount::new(128));
        assert_eq!(
            from_value::<ToolInvocationCapabilities>(to_value(&invocation).unwrap()).unwrap(),
            invocation
        );
        assert!(
            from_value::<ToolInvocationCapabilities>(json!({
                "cancellation": "authoritative",
                "max_progress_events": "128"
            }))
            .is_err()
        );
    }

    #[test]
    fn execution_limits_are_finite_consistent_and_closed() {
        let limits = default_limits();
        assert_eq!(limits.timeout(), DurationMillis::new(30_000).unwrap());
        assert_eq!(limits.max_concurrency(), ExecutionCount::new(16));
        assert_eq!(limits.max_input_bytes(), ByteCount::new(64 * 1024));
        assert_eq!(limits.max_inline_result_bytes(), ByteCount::new(256 * 1024));
        assert_eq!(limits.max_artifacts(), ExecutionCount::new(4));
        assert_eq!(
            limits.max_total_artifact_bytes(),
            ByteCount::new(25 * 1024 * 1024)
        );

        let encoded = to_value(&limits).unwrap();
        assert_eq!(encoded["timeout"], "30000");
        assert_eq!(encoded["max_concurrency"], "16");
        assert_eq!(encoded["max_input_bytes"], "65536");
        assert_eq!(encoded["max_inline_result_bytes"], "262144");
        assert_eq!(encoded["max_artifacts"], "4");
        assert_eq!(encoded["max_total_artifact_bytes"], "26214400");
        assert_eq!(
            from_value::<ToolExecutionLimits>(encoded.clone()).unwrap(),
            limits
        );

        assert_eq!(
            ToolExecutionLimits::new(
                DurationMillis::ZERO,
                ExecutionCount::new(1),
                ByteCount::new(1),
                ByteCount::new(1),
                ExecutionCount::ZERO,
                ByteCount::ZERO,
            ),
            Err(ToolExecutionLimitsError::ZeroTimeout)
        );
        assert_eq!(
            ToolExecutionLimits::new(
                DurationMillis::new(1).unwrap(),
                ExecutionCount::ZERO,
                ByteCount::new(1),
                ByteCount::new(1),
                ExecutionCount::ZERO,
                ByteCount::ZERO,
            ),
            Err(ToolExecutionLimitsError::ZeroConcurrency)
        );
        assert_eq!(
            ToolExecutionLimits::new(
                DurationMillis::new(1).unwrap(),
                ExecutionCount::new(1),
                ByteCount::ZERO,
                ByteCount::new(1),
                ExecutionCount::ZERO,
                ByteCount::ZERO,
            ),
            Err(ToolExecutionLimitsError::ZeroInputBytes)
        );
        assert_eq!(
            ToolExecutionLimits::new(
                DurationMillis::new(1).unwrap(),
                ExecutionCount::new(1),
                ByteCount::new(1),
                ByteCount::ZERO,
                ExecutionCount::ZERO,
                ByteCount::ZERO,
            ),
            Err(ToolExecutionLimitsError::ZeroInlineResultBytes)
        );
        for (max_artifacts, max_total_artifact_bytes) in [
            (ExecutionCount::new(1), ByteCount::ZERO),
            (ExecutionCount::ZERO, ByteCount::new(1)),
        ] {
            assert_eq!(
                ToolExecutionLimits::new(
                    DurationMillis::new(1).unwrap(),
                    ExecutionCount::new(1),
                    ByteCount::new(1),
                    ByteCount::new(1),
                    max_artifacts,
                    max_total_artifact_bytes,
                ),
                Err(ToolExecutionLimitsError::ArtifactCapacityMismatch {
                    max_artifacts,
                    max_total_artifact_bytes,
                })
            );
        }

        let mut unknown = encoded;
        unknown["unlimited"] = json!(true);
        assert!(from_value::<ToolExecutionLimits>(unknown).is_err());
    }

    #[test]
    fn descriptors_revalidate_kind_resources_and_redact_discovery_text() {
        let secret = "descriptor-secret";
        let descriptor = descriptor(secret);
        assert_eq!(descriptor.metadata().kind(), CapabilityKind::Tool);
        assert_eq!(
            descriptor.input_schema().id().as_str(),
            "https://schemas.example.com/tools/capture-input/1.0.0"
        );
        assert_eq!(
            descriptor.output_schema().id().as_str(),
            "https://schemas.example.com/tools/capture-output/1.0.0"
        );
        assert!(descriptor.semantics().requires_idempotency_key());
        assert!(descriptor.resources().requires_credentials());
        assert!(descriptor.invocation().supports_progress_events());
        assert_eq!(descriptor.limits(), &default_limits());
        assert!(!format!("{descriptor:?}").contains(secret));

        let encoded = to_value(&descriptor).unwrap();
        assert_eq!(
            from_value::<ToolDescriptor>(encoded.clone()).unwrap(),
            descriptor
        );

        assert_eq!(
            ToolDescriptor::new(
                metadata(CapabilityKind::Agent, secret),
                schema("input"),
                schema("output"),
                read_only_semantics(),
                ToolResourceRequirements::none(),
                ToolInvocationCapabilities::new(
                    ToolCancellationSupport::Unsupported,
                    ExecutionCount::ZERO,
                ),
                default_limits(),
            ),
            Err(ToolDescriptorError::WrongCapabilityKind {
                actual: CapabilityKind::Agent,
            })
        );

        let write_resources = ToolResourceRequirements::new(
            ToolResourceAccess::ReadWrite,
            ToolResourceAccess::None,
            false,
            false,
        );
        assert_eq!(
            ToolDescriptor::new(
                metadata(CapabilityKind::Tool, secret),
                schema("input"),
                schema("output"),
                read_only_semantics(),
                write_resources,
                ToolInvocationCapabilities::new(
                    ToolCancellationSupport::Unsupported,
                    ExecutionCount::ZERO,
                ),
                default_limits(),
            ),
            Err(ToolDescriptorError::ReadOnlyResourceWrite {
                network: ToolResourceAccess::ReadWrite,
                filesystem: ToolResourceAccess::None,
            })
        );

        let mut wrong_kind = encoded.clone();
        wrong_kind["metadata"]["kind"] = json!("agent");
        assert!(from_value::<ToolDescriptor>(wrong_kind).is_err());
        let mut unsafe_resources = encoded.clone();
        unsafe_resources["semantics"] = to_value(read_only_semantics()).unwrap();
        assert!(from_value::<ToolDescriptor>(unsafe_resources).is_err());
        let mut unknown = encoded;
        unknown["provider"] = json!("remote");
        assert!(from_value::<ToolDescriptor>(unknown).is_err());
    }

    #[test]
    fn tool_contract_schemas_are_closed_and_require_every_semantic_field() {
        for schema in [
            to_value(schemars::schema_for!(ToolExecutionSemantics)).unwrap(),
            to_value(schemars::schema_for!(ToolResourceRequirements)).unwrap(),
            to_value(schemars::schema_for!(ToolInvocationCapabilities)).unwrap(),
            to_value(schemars::schema_for!(ToolExecutionLimits)).unwrap(),
            to_value(schemars::schema_for!(ToolDescriptor)).unwrap(),
        ] {
            assert_eq!(schema["type"], "object");
            assert_eq!(schema["additionalProperties"], false);
        }

        let descriptor_schema = to_value(schemars::schema_for!(ToolDescriptor)).unwrap();
        assert_eq!(
            descriptor_schema["required"],
            json!([
                "metadata",
                "input_schema",
                "output_schema",
                "semantics",
                "resources",
                "invocation",
                "limits"
            ])
        );
    }

    proptest! {
        #[test]
        fn every_positive_limit_tuple_round_trips_without_widening(
            timeout in 1_i64..=1_000_000_000_i64,
            concurrency in 1_u64..=1_000_000_u64,
            input_bytes in 1_u64..=1_000_000_000_u64,
            inline_bytes in 1_u64..=1_000_000_000_u64,
            artifact_count in 0_u64..=1_000_u64,
            artifact_bytes in 1_u64..=1_000_000_000_u64,
        ) {
            let artifact_bytes = if artifact_count == 0 { 0 } else { artifact_bytes };
            let limits = ToolExecutionLimits::new(
                DurationMillis::new(timeout).unwrap(),
                ExecutionCount::new(concurrency),
                ByteCount::new(input_bytes),
                ByteCount::new(inline_bytes),
                ExecutionCount::new(artifact_count),
                ByteCount::new(artifact_bytes),
            ).unwrap();
            let encoded = serde_json::to_vec(&limits).unwrap();
            let decoded = serde_json::from_slice::<ToolExecutionLimits>(&encoded).unwrap();
            prop_assert_eq!(decoded, limits);
        }
    }
}