Skip to main content

rustev_contract/
limits.rs

1//! The named parse limit sets of spec 002, 3.2.5. Each document type uses
2//! exactly one of them through [`crate::Document::LIMITS`].
3
4use crate::bounded::ParseLimits;
5
6const KIB: usize = 1024;
7const MIB: usize = 1024 * KIB;
8
9/// Decision definitions.
10pub const DEFINITION_V1: ParseLimits = ParseLimits {
11    max_bytes: MIB,
12    max_depth: 32,
13    max_string_bytes: 64 * KIB,
14    max_collection_len: 4096,
15    max_total_values: 100_000,
16    max_number_bytes: 40,
17    allow_fractional_numbers: false,
18};
19
20/// Context snapshots.
21pub const SNAPSHOT_V1: ParseLimits = ParseLimits {
22    max_bytes: 4 * MIB,
23    max_depth: 32,
24    max_string_bytes: 256 * KIB,
25    max_collection_len: 10_000,
26    max_total_values: 500_000,
27    max_number_bytes: 40,
28    allow_fractional_numbers: false,
29};
30
31/// Backend descriptors and calibration artifacts.
32pub const DESCRIPTOR_V1: ParseLimits = ParseLimits {
33    max_bytes: 256 * KIB,
34    max_depth: 16,
35    max_string_bytes: 4 * KIB,
36    max_collection_len: 1024,
37    max_total_values: 20_000,
38    max_number_bytes: 40,
39    allow_fractional_numbers: false,
40};
41
42/// Backend outputs; masses, logits and scores are JSON numbers.
43pub const BACKEND_OUTPUT_V1: ParseLimits = ParseLimits {
44    max_bytes: MIB,
45    max_depth: 8,
46    max_string_bytes: 4 * KIB,
47    max_collection_len: 4096,
48    max_total_values: 50_000,
49    max_number_bytes: 40,
50    allow_fractional_numbers: true,
51};
52
53/// Compiled plans.
54pub const PLAN_V1: ParseLimits = ParseLimits {
55    max_bytes: 2 * MIB,
56    max_depth: 32,
57    max_string_bytes: 64 * KIB,
58    max_collection_len: 4096,
59    max_total_values: 200_000,
60    max_number_bytes: 40,
61    allow_fractional_numbers: false,
62};
63
64/// Judgments, evidence records and evaluation reports.
65pub const RECORD_V1: ParseLimits = ParseLimits {
66    max_bytes: 4 * MIB,
67    max_depth: 32,
68    max_string_bytes: 256 * KIB,
69    max_collection_len: 10_000,
70    max_total_values: 500_000,
71    max_number_bytes: 40,
72    allow_fractional_numbers: true,
73};
74
75/// Replay bundles and eval-owned documents (spec 004, 3.2 and 3.5).
76pub const REPLAY_V1: ParseLimits = ParseLimits {
77    max_bytes: 16 * MIB,
78    max_depth: 48,
79    max_string_bytes: 8 * MIB,
80    max_collection_len: 10_000,
81    max_total_values: 1_000_000,
82    max_number_bytes: 40,
83    allow_fractional_numbers: true,
84};
85
86/// Request identity documents: `REPLAY_V1` tightened to 4 MiB (spec 004,
87/// 3.4.2). The projection inside still obeys the plan's own bound.
88pub const REQUEST_V1: ParseLimits = ParseLimits {
89    max_bytes: 4 * MIB,
90    ..REPLAY_V1
91};
92
93/// The `rustev.remote/1` documents (spec 009, 3.10): descriptors inside a
94/// describe answer, projections as strings, outputs as JSON numbers. A
95/// transport also bounds what it reads by the negotiated maximum.
96pub const REMOTE_V1: ParseLimits = ParseLimits {
97    max_bytes: 4 * MIB,
98    max_depth: 16,
99    max_string_bytes: MIB,
100    max_collection_len: 4096,
101    max_total_values: 100_000,
102    max_number_bytes: 40,
103    allow_fractional_numbers: true,
104};
105
106/// Exchange records, `rustev.remote-exchange/1` (spec 009, 3.9.1): a fixed
107/// maximum size, with free text cut to 256 bytes before escaping.
108pub const EXCHANGE_V1: ParseLimits = ParseLimits {
109    max_bytes: 512 * KIB,
110    max_depth: 8,
111    max_string_bytes: 2 * KIB,
112    max_collection_len: 64,
113    max_total_values: 4096,
114    max_number_bytes: 40,
115    allow_fractional_numbers: false,
116};
117
118/// Dynamic questions, `rustev.dynamic-question/1` (spec 019, 3.2): the
119/// request's own bounds (128 KiB canonical, 64 KiB of context values) are
120/// checked after this scan; the scan admits at most twice the canonical
121/// bound in raw bytes, so whitespace and escapes cannot force an unbounded
122/// allocation.
123pub const DYNAMIC_QUESTION_V1: ParseLimits = ParseLimits {
124    max_bytes: 256 * KIB,
125    max_depth: 8,
126    max_string_bytes: 128 * KIB,
127    max_collection_len: 64,
128    max_total_values: 4096,
129    max_number_bytes: 40,
130    allow_fractional_numbers: false,
131};
132
133/// Span lines, `rustev.trace/1` (spec 028, 3.10.1): at most 64 KiB per
134/// line. An attribute value is at most 4 096 bytes before escaping, so a
135/// string's raw length is bounded at six times that.
136pub const TRACE_V1: ParseLimits = ParseLimits {
137    max_bytes: 64 * KIB,
138    max_depth: 8,
139    max_string_bytes: 24 * KIB,
140    max_collection_len: 1024,
141    max_total_values: 20_000,
142    max_number_bytes: 40,
143    allow_fractional_numbers: true,
144};