torg_core/limits.rs
1//! Resource limits for DoS prevention.
2
3/// Resource limits for graph construction.
4///
5/// These limits prevent excessive resource consumption during
6/// graph construction, providing DoS protection when processing
7/// untrusted token streams.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub struct Limits {
10 /// Maximum number of nodes in the graph.
11 pub max_nodes: usize,
12 /// Maximum depth of the DAG (longest path from input to output).
13 pub max_depth: usize,
14 /// Maximum number of input declarations.
15 pub max_inputs: usize,
16 /// Maximum number of output declarations.
17 pub max_outputs: usize,
18}
19
20impl Default for Limits {
21 fn default() -> Self {
22 Self {
23 max_nodes: 256,
24 max_depth: 64,
25 max_inputs: 128,
26 max_outputs: 32,
27 }
28 }
29}
30
31impl Limits {
32 /// Create limits with custom values.
33 pub fn new(max_nodes: usize, max_depth: usize, max_inputs: usize, max_outputs: usize) -> Self {
34 Self {
35 max_nodes,
36 max_depth,
37 max_inputs,
38 max_outputs,
39 }
40 }
41
42 /// Permissive limits for testing.
43 pub fn permissive() -> Self {
44 Self {
45 max_nodes: 4096,
46 max_depth: 256,
47 max_inputs: 1024,
48 max_outputs: 256,
49 }
50 }
51
52 /// Strict limits for production LLM use.
53 pub fn strict() -> Self {
54 Self {
55 max_nodes: 64,
56 max_depth: 16,
57 max_inputs: 32,
58 max_outputs: 8,
59 }
60 }
61}