rig-compose 0.5.0

Composable agent kernel: stateless skills, transport-agnostic tools, registry-driven agents, signal-routing coordinator. Companion crate for rig.
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
//! [`Tool`] — the only side-effectful interface available to skills and agents.
//!
//! A [`Tool`] is a typed, named, async function with a JSON-Schema-compatible
//! signature. Two transports satisfy the trait today: [`LocalTool`] (a closure
//! over a Rust async fn) and — under the `mcp` feature in a later phase — a
//! remote MCP server. Skills never know the difference.

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};

use crate::registry::KernelError;

/// Stable, registry-unique identifier for a tool (e.g. `"grammar.query"`,
/// `"memory.lookup"`, `"sampler.expand"`).
pub type ToolName = String;

/// Lightweight description of a tool's I/O contract. The `args_schema` and
/// `result_schema` are JSON-Schema fragments; the LLM-facing rendering layer
/// uses them to generate `rig` / MCP tool definitions automatically. We do
/// **not** validate against them at the kernel — validation is the tool's
/// responsibility — but downstream MCP exporters need them.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ToolSchema {
    pub name: ToolName,
    pub description: String,
    pub args_schema: Value,
    pub result_schema: Value,
}

/// Configuration for bounding large tool results before they enter a model
/// turn, trace record, or MCP response cache.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ToolResultEnvelopeConfig {
    /// Maximum characters retained for any string value.
    pub max_string_chars: usize,
    /// Maximum items retained for any array value.
    pub max_array_items: usize,
    /// Maximum serialized bytes retained for the entire bounded payload.
    pub max_total_bytes: usize,
    /// Optional redaction policy applied before size bounding.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub redaction: Option<RedactionPolicy>,
}

impl Default for ToolResultEnvelopeConfig {
    fn default() -> Self {
        Self {
            max_string_chars: 4_000,
            max_array_items: 64,
            max_total_bytes: 256_000,
            redaction: None,
        }
    }
}

impl ToolResultEnvelopeConfig {
    /// Build a config with a string character limit and otherwise default
    /// limits.
    #[must_use]
    pub fn new(max_string_chars: usize) -> Self {
        Self {
            max_string_chars,
            ..Self::default()
        }
    }

    /// Set the maximum retained array items.
    #[must_use]
    pub fn with_max_array_items(mut self, max_array_items: usize) -> Self {
        self.max_array_items = max_array_items;
        self
    }

    /// Set the maximum serialized bytes retained for the whole payload.
    #[must_use]
    pub fn with_max_total_bytes(mut self, max_total_bytes: usize) -> Self {
        self.max_total_bytes = max_total_bytes;
        self
    }

    /// Set the redaction policy applied before truncation and total bounding.
    #[must_use]
    pub fn with_redaction_policy(mut self, redaction: RedactionPolicy) -> Self {
        self.redaction = Some(redaction);
        self
    }
}

/// One JSON Pointer redaction rule.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RedactionRule {
    /// Canonical JSON Pointer to redact, for example `/secret/token`.
    pub pointer: String,
    /// Replacement string for this pointer. Falls back to the policy default.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub replacement: Option<String>,
}

/// JSON Pointer redaction policy for tool results.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RedactionPolicy {
    /// Pointers redacted before size bounding.
    pub deny: Vec<RedactionRule>,
    /// Optional allow-list. Values outside these pointers are redacted while
    /// ancestor containers are retained so allowed descendants can survive.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allow: Option<Vec<String>>,
    /// Default replacement used when a rule does not specify one.
    pub default_replacement: String,
}

impl RedactionPolicy {
    /// Create a deny-list policy from JSON Pointers.
    #[must_use]
    pub fn deny_pointers<I, S>(pointers: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self {
            deny: pointers
                .into_iter()
                .map(|pointer| RedactionRule {
                    pointer: pointer.into(),
                    replacement: None,
                })
                .collect(),
            allow: None,
            default_replacement: "[redacted]".to_string(),
        }
    }

    /// Create an allow-list policy from JSON Pointers.
    #[must_use]
    pub fn allow_pointers<I, S>(pointers: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self {
            deny: Vec::new(),
            allow: Some(pointers.into_iter().map(Into::into).collect()),
            default_replacement: "[redacted]".to_string(),
        }
    }

    /// Set the default replacement string.
    #[must_use]
    pub fn with_default_replacement(mut self, replacement: impl Into<String>) -> Self {
        self.default_replacement = replacement.into();
        self
    }

    /// Set a replacement string for one deny-list pointer.
    #[must_use]
    pub fn with_replacement(
        mut self,
        pointer: impl Into<String>,
        replacement: impl Into<String>,
    ) -> Self {
        let pointer = pointer.into();
        let replacement = replacement.into();
        for rule in &mut self.deny {
            if rule.pointer == pointer {
                rule.replacement = Some(replacement);
                return self;
            }
        }
        self.deny.push(RedactionRule {
            pointer,
            replacement: Some(replacement),
        });
        self
    }
}

/// Reason an individual payload segment was omitted from a tool result envelope.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ToolResultOmissionReason {
    /// A string exceeded `max_string_chars`.
    StringChars,
    /// An array exceeded `max_array_items` or the total byte budget.
    ArrayItems,
    /// An object field or scalar exceeded `max_total_bytes`.
    TotalBytes,
}

/// One omitted payload segment and its opaque continuation token.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct OmittedSegment {
    /// JSON Pointer to the omitted segment in the original payload.
    pub pointer: String,
    /// Machine-readable omission reason.
    pub reason: ToolResultOmissionReason,
    /// Opaque continuation token for callers that can page the original result.
    pub page_token: String,
}

/// Decoded continuation token metadata for a bounded tool result segment.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ToolResultPageToken {
    /// JSON Pointer to the omitted segment in the original payload.
    pub pointer: String,
    /// Machine-readable omission reason.
    pub reason: ToolResultOmissionReason,
    /// Limit that caused the omission.
    pub limit: usize,
}

/// Decode a [`ToolResultEnvelope`] page token produced by this crate.
#[must_use]
pub fn decode_tool_result_page_token(token: &str) -> Option<ToolResultPageToken> {
    let payload = token.strip_prefix("v1:")?;
    let bytes = decode_hex(payload)?;
    let text = String::from_utf8(bytes).ok()?;
    serde_json::from_str(&text).ok()
}

/// Tool result plus deterministic truncation metadata.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ToolResultEnvelope {
    /// Possibly bounded result payload.
    pub payload: Value,
    /// Whether any value was truncated or omitted.
    pub truncated: bool,
    /// Total string characters omitted while bounding the payload.
    pub omitted_chars: usize,
    /// Total array items omitted while bounding the payload.
    pub omitted_items: usize,
    /// Total whole values omitted while enforcing the total byte budget.
    #[serde(default, skip_serializing_if = "is_zero")]
    pub omitted_values: usize,
    /// Total values replaced by the redaction policy.
    #[serde(default, skip_serializing_if = "is_zero")]
    pub redacted_values: usize,
    /// Every omitted segment recorded during bounding.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub omitted_segments: Vec<OmittedSegment>,
    /// Stable follow-up token describing the first omitted segment.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_token: Option<String>,
}

impl ToolResultEnvelope {
    /// Bound `payload` according to `config` and return truncation metadata.
    #[must_use]
    pub fn bound(payload: Value, config: &ToolResultEnvelopeConfig) -> Self {
        let mut state = ToolResultEnvelopeState::default();
        let payload = bound_value(payload, config, &mut state, "");
        let payload = bound_total_bytes(payload, config, &mut state, "");
        Self {
            payload,
            truncated: state.omitted_chars > 0
                || state.omitted_items > 0
                || state.omitted_values > 0,
            omitted_chars: state.omitted_chars,
            omitted_items: state.omitted_items,
            omitted_values: state.omitted_values,
            redacted_values: state.redacted_values,
            omitted_segments: state.omitted_segments,
            page_token: state.page_token,
        }
    }
}

fn is_zero(value: &usize) -> bool {
    *value == 0
}

/// Bound `payload` with the default [`ToolResultEnvelopeConfig`].
#[must_use]
pub fn bound_tool_result(payload: Value) -> ToolResultEnvelope {
    ToolResultEnvelope::bound(payload, &ToolResultEnvelopeConfig::default())
}

#[derive(Default)]
struct ToolResultEnvelopeState {
    omitted_chars: usize,
    omitted_items: usize,
    omitted_values: usize,
    redacted_values: usize,
    omitted_segments: Vec<OmittedSegment>,
    page_token: Option<String>,
}

impl ToolResultEnvelopeState {
    fn record_omission(&mut self, pointer: &str, reason: ToolResultOmissionReason, limit: usize) {
        let page_token = page_token(pointer, reason, limit);
        if self.page_token.is_none() {
            self.page_token = Some(page_token.clone());
        }
        self.omitted_segments.push(OmittedSegment {
            pointer: pointer.to_string(),
            reason,
            page_token,
        });
    }
}

fn bound_value(
    value: Value,
    config: &ToolResultEnvelopeConfig,
    state: &mut ToolResultEnvelopeState,
    pointer: &str,
) -> Value {
    if let Some(redaction) = &config.redaction
        && let Some(replacement) = redaction.replacement_for(pointer)
    {
        state.redacted_values = state.redacted_values.saturating_add(1);
        return Value::String(replacement);
    }

    match value {
        Value::String(text) => bound_string(text, config, state, pointer),
        Value::Array(items) => bound_array(items, config, state, pointer),
        Value::Object(fields) => bound_object(fields, config, state, pointer),
        scalar => scalar,
    }
}

fn bound_string(
    text: String,
    config: &ToolResultEnvelopeConfig,
    state: &mut ToolResultEnvelopeState,
    pointer: &str,
) -> Value {
    let total_chars = text.chars().count();
    if total_chars <= config.max_string_chars {
        return Value::String(text);
    }
    state.omitted_chars = state
        .omitted_chars
        .saturating_add(total_chars.saturating_sub(config.max_string_chars));
    state.record_omission(
        pointer,
        ToolResultOmissionReason::StringChars,
        config.max_string_chars,
    );
    Value::String(text.chars().take(config.max_string_chars).collect())
}

fn bound_array(
    items: Vec<Value>,
    config: &ToolResultEnvelopeConfig,
    state: &mut ToolResultEnvelopeState,
    pointer: &str,
) -> Value {
    let total_items = items.len();
    if total_items > config.max_array_items {
        state.omitted_items = state
            .omitted_items
            .saturating_add(total_items.saturating_sub(config.max_array_items));
        state.record_omission(
            pointer,
            ToolResultOmissionReason::ArrayItems,
            config.max_array_items,
        );
    }
    Value::Array(
        items
            .into_iter()
            .enumerate()
            .take(config.max_array_items)
            .map(|(index, item)| {
                let child = child_pointer(pointer, &index.to_string());
                bound_value(item, config, state, &child)
            })
            .collect(),
    )
}

fn bound_object(
    fields: Map<String, Value>,
    config: &ToolResultEnvelopeConfig,
    state: &mut ToolResultEnvelopeState,
    pointer: &str,
) -> Value {
    Value::Object(
        fields
            .into_iter()
            .map(|(key, value)| {
                let child = child_pointer(pointer, &key);
                (key, bound_value(value, config, state, &child))
            })
            .collect(),
    )
}

impl RedactionPolicy {
    fn replacement_for(&self, pointer: &str) -> Option<String> {
        for rule in &self.deny {
            if rule.pointer == pointer {
                return Some(
                    rule.replacement
                        .clone()
                        .unwrap_or_else(|| self.default_replacement.clone()),
                );
            }
        }

        let Some(allow) = &self.allow else {
            return None;
        };
        if allow
            .iter()
            .any(|allowed| pointer_matches(pointer, allowed))
        {
            return None;
        }
        Some(self.default_replacement.clone())
    }
}

fn pointer_matches(pointer: &str, allowed: &str) -> bool {
    pointer == allowed || is_descendant(pointer, allowed) || is_descendant(allowed, pointer)
}

fn is_descendant(pointer: &str, ancestor: &str) -> bool {
    if ancestor.is_empty() {
        return !pointer.is_empty();
    }
    let prefix = format!("{ancestor}/");
    pointer.starts_with(&prefix)
}

fn child_pointer(parent: &str, child: &str) -> String {
    let escaped = escape_pointer_segment(child);
    if parent.is_empty() {
        format!("/{escaped}")
    } else {
        format!("{parent}/{escaped}")
    }
}

fn escape_pointer_segment(segment: &str) -> String {
    segment.replace('~', "~0").replace('/', "~1")
}

fn page_token(pointer: &str, reason: ToolResultOmissionReason, limit: usize) -> String {
    let payload = ToolResultPageToken {
        pointer: pointer.to_string(),
        reason,
        limit,
    };
    match serde_json::to_string(&payload) {
        Ok(serialized) => format!("v1:{}", encode_hex(serialized.as_bytes())),
        Err(_) => "v1:".to_string(),
    }
}

fn encode_hex(bytes: &[u8]) -> String {
    bytes.iter().map(|byte| format!("{byte:02x}")).collect()
}

fn decode_hex(input: &str) -> Option<Vec<u8>> {
    let mut chars = input.chars();
    let mut bytes = Vec::new();
    loop {
        let Some(high) = chars.next() else {
            return Some(bytes);
        };
        let low = chars.next()?;
        let high = hex_value(high)?;
        let low = hex_value(low)?;
        bytes.push(high.saturating_mul(16).saturating_add(low));
    }
}

fn hex_value(character: char) -> Option<u8> {
    match character {
        '0'..='9' => Some(character as u8 - b'0'),
        'a'..='f' => Some(character as u8 - b'a' + 10),
        'A'..='F' => Some(character as u8 - b'A' + 10),
        _ => None,
    }
}

fn bound_total_bytes(
    value: Value,
    config: &ToolResultEnvelopeConfig,
    state: &mut ToolResultEnvelopeState,
    pointer: &str,
) -> Value {
    if serialized_len(&value) <= config.max_total_bytes {
        return value;
    }

    match value {
        Value::Object(fields) => bound_object_total_bytes(fields, config, state, pointer),
        Value::Array(items) => bound_array_total_bytes(items, config, state, pointer),
        Value::String(text) => bound_string_total_bytes(text, config, state, pointer),
        scalar => {
            state.omitted_values = state.omitted_values.saturating_add(1);
            state.record_omission(
                pointer,
                ToolResultOmissionReason::TotalBytes,
                config.max_total_bytes,
            );
            scalar
        }
    }
}

fn bound_object_total_bytes(
    fields: Map<String, Value>,
    config: &ToolResultEnvelopeConfig,
    state: &mut ToolResultEnvelopeState,
    pointer: &str,
) -> Value {
    let mut retained = Map::new();
    for (key, value) in fields {
        let child = child_pointer(pointer, &key);
        let mut candidate = retained.clone();
        candidate.insert(key.clone(), value.clone());
        if serialized_len(&Value::Object(candidate)) <= config.max_total_bytes {
            retained.insert(key, value);
        } else {
            state.omitted_values = state.omitted_values.saturating_add(1);
            state.record_omission(
                &child,
                ToolResultOmissionReason::TotalBytes,
                config.max_total_bytes,
            );
        }
    }
    Value::Object(retained)
}

fn bound_array_total_bytes(
    items: Vec<Value>,
    config: &ToolResultEnvelopeConfig,
    state: &mut ToolResultEnvelopeState,
    pointer: &str,
) -> Value {
    let mut retained = Vec::new();
    for (index, item) in items.into_iter().enumerate() {
        let mut candidate = retained.clone();
        candidate.push(item.clone());
        if serialized_len(&Value::Array(candidate)) <= config.max_total_bytes {
            retained.push(item);
        } else {
            state.omitted_items = state.omitted_items.saturating_add(1);
            let child = child_pointer(pointer, &index.to_string());
            state.record_omission(
                &child,
                ToolResultOmissionReason::TotalBytes,
                config.max_total_bytes,
            );
        }
    }
    Value::Array(retained)
}

fn bound_string_total_bytes(
    text: String,
    config: &ToolResultEnvelopeConfig,
    state: &mut ToolResultEnvelopeState,
    pointer: &str,
) -> Value {
    let mut retained = String::new();
    for character in text.chars() {
        let mut candidate = retained.clone();
        candidate.push(character);
        if serialized_len(&Value::String(candidate)) <= config.max_total_bytes {
            retained.push(character);
        } else {
            state.omitted_chars = state.omitted_chars.saturating_add(1);
        }
    }
    if retained.chars().count() < text.chars().count() {
        state.record_omission(
            pointer,
            ToolResultOmissionReason::TotalBytes,
            config.max_total_bytes,
        );
    }
    Value::String(retained)
}

fn serialized_len(value: &Value) -> usize {
    match serde_json::to_string(value) {
        Ok(serialized) => serialized.len(),
        Err(_) => usize::MAX,
    }
}

/// A composable, side-effectful capability.
///
/// Implementations MUST be cheap to clone (typically `Arc`-wrapped state) so
/// the same tool instance can be referenced from multiple agents'
/// [`super::registry::ToolRegistry`] slices.
#[async_trait]
pub trait Tool: Send + Sync {
    /// Return this tool's JSON-Schema-compatible contract.
    fn schema(&self) -> ToolSchema;

    /// Return this tool's registry name.
    fn name(&self) -> ToolName {
        self.schema().name
    }

    /// Invoke the tool with JSON arguments.
    async fn invoke(&self, args: Value) -> Result<Value, KernelError>;
}

/// Adapter that turns any `async Fn(Value) -> Result<Value, KernelError>`
/// into a [`Tool`]. Hosts can use this to surface existing async functions
/// to the kernel without writing a dedicated tool type.
pub struct LocalTool {
    schema: ToolSchema,
    #[allow(clippy::type_complexity)]
    f: Arc<
        dyn Fn(Value) -> Pin<Box<dyn Future<Output = Result<Value, KernelError>> + Send>>
            + Send
            + Sync,
    >,
}

impl LocalTool {
    pub fn new<F, Fut>(schema: ToolSchema, f: F) -> Self
    where
        F: Fn(Value) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<Value, KernelError>> + Send + 'static,
    {
        Self {
            schema,
            f: Arc::new(move |v| Box::pin(f(v))),
        }
    }
}

#[async_trait]
impl Tool for LocalTool {
    fn schema(&self) -> ToolSchema {
        self.schema.clone()
    }

    fn name(&self) -> ToolName {
        self.schema.name.clone()
    }

    async fn invoke(&self, args: Value) -> Result<Value, KernelError> {
        (self.f)(args).await
    }
}

#[cfg(test)]
mod tests {
    use crate::*;
    use serde_json::json;

    #[tokio::test]
    async fn local_tool_roundtrip() {
        let schema = ToolSchema {
            name: "test.echo".into(),
            description: "echoes the input".into(),
            args_schema: json!({"type": "object"}),
            result_schema: json!({"type": "object"}),
        };
        let tool = LocalTool::new(schema, |v| async move { Ok(v) });
        let out = tool.invoke(json!({"hello": "world"})).await.unwrap();
        assert_eq!(out, json!({"hello": "world"}));
        assert_eq!(tool.name(), "test.echo");
    }

    #[test]
    fn tool_result_envelope_bounds_large_strings() {
        let envelope =
            ToolResultEnvelope::bound(json!({"body": "abcdef"}), &ToolResultEnvelopeConfig::new(3));

        assert_eq!(envelope.payload, json!({"body": "abc"}));
        assert!(envelope.truncated);
        assert_eq!(envelope.omitted_chars, 3);
        assert_eq!(
            decode_tool_result_page_token(envelope.page_token.as_deref().unwrap()),
            Some(ToolResultPageToken {
                pointer: "/body".to_string(),
                reason: ToolResultOmissionReason::StringChars,
                limit: 3,
            })
        );
        assert_eq!(envelope.omitted_segments.len(), 1);
        assert!(
            envelope
                .omitted_segments
                .iter()
                .any(|segment| segment.pointer == "/body")
        );
    }

    #[test]
    fn tool_result_envelope_bounds_arrays() {
        let envelope = ToolResultEnvelope::bound(
            json!({"rows": [1, 2, 3, 4]}),
            &ToolResultEnvelopeConfig::new(100).with_max_array_items(2),
        );

        assert_eq!(envelope.payload, json!({"rows": [1, 2]}));
        assert!(envelope.truncated);
        assert_eq!(envelope.omitted_items, 2);
        assert_eq!(
            decode_tool_result_page_token(envelope.page_token.as_deref().unwrap()),
            Some(ToolResultPageToken {
                pointer: "/rows".to_string(),
                reason: ToolResultOmissionReason::ArrayItems,
                limit: 2,
            })
        );
        assert_eq!(envelope.omitted_segments.len(), 1);
        assert!(
            envelope
                .omitted_segments
                .iter()
                .any(|segment| segment.pointer == "/rows")
        );
    }

    #[test]
    fn tool_result_envelope_leaves_small_payloads_unchanged() {
        let payload = json!({"ok": true, "rows": ["a"]});
        let envelope = ToolResultEnvelope::bound(
            payload.clone(),
            &ToolResultEnvelopeConfig::new(100).with_max_array_items(10),
        );

        assert_eq!(envelope.payload, payload);
        assert!(!envelope.truncated);
        assert_eq!(envelope.omitted_chars, 0);
        assert_eq!(envelope.omitted_items, 0);
        assert_eq!(envelope.page_token, None);
    }

    #[test]
    fn tool_result_envelope_redacts_before_truncation() {
        let config = ToolResultEnvelopeConfig::new(4).with_redaction_policy(
            RedactionPolicy::deny_pointers(["/secret"]).with_replacement("/secret", "safe"),
        );
        let envelope = ToolResultEnvelope::bound(
            json!({"public": "abcdef", "secret": "should-not-leak"}),
            &config,
        );

        assert_eq!(
            envelope.payload,
            json!({"public": "abcd", "secret": "safe"})
        );
        assert_eq!(envelope.redacted_values, 1);
        assert_eq!(envelope.omitted_chars, 2);
        assert_eq!(
            decode_tool_result_page_token(envelope.page_token.as_deref().unwrap()),
            Some(ToolResultPageToken {
                pointer: "/public".to_string(),
                reason: ToolResultOmissionReason::StringChars,
                limit: 4,
            })
        );
    }

    #[test]
    fn tool_result_envelope_total_budget_drops_fields_with_path_tokens() {
        let config = ToolResultEnvelopeConfig::new(100).with_max_total_bytes(24);
        let envelope = ToolResultEnvelope::bound(
            json!({"a": "small", "b": "also-small", "c": "extra"}),
            &config,
        );

        assert!(envelope.truncated);
        assert!(envelope.omitted_values > 0);
        assert!(envelope.payload.get("a").is_some());
        assert!(envelope.omitted_segments.iter().any(|segment| {
            segment.reason == ToolResultOmissionReason::TotalBytes
                && decode_tool_result_page_token(&segment.page_token).is_some_and(|token| {
                    token.reason == ToolResultOmissionReason::TotalBytes && token.limit == 24
                })
        }));
    }
}