boundgraph-slice 0.1.0

Layer 4 of BoundGraph: the slicers that turn raw tool output into size-capped condensates.
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
//! Condensing JSON: keep the shape, sample the values.
//!
//! # Why the schema is the part worth keeping
//!
//! A condensate is typed, not a blob of text (US-0104), and for an API response
//! the type *is* the useful part. A model that knows the response is
//! `{ users: [{ id: integer, email: string }], next_cursor: string|null }` can
//! plan its next step without reading five thousand users. One shown the first
//! two hundred users and nothing else has to guess whether `next_cursor` exists
//! at all.
//!
//! So the schema is retained unconditionally and the values compete for what is
//! left. Under a cap that fits nothing else, a JSON condensate is still a
//! complete and accurate description of the response's shape.
//!
//! # Secrets are removed, not condensed
//!
//! Redaction happens before anything else and is not subject to the cap. A
//! token that fits is *worse* than one that does not: it reaches the model, the
//! ledger, and any sink the ledger feeds (US-1004 exports spans to a collector).
//! `ARCHITECTURE.md` §11 requires secrets never to be journalled and US-1005
//! asks for a redaction test to prove it; this is the layer where API responses
//! get that treatment.
//!
//! Matching is by field name against the conventions credentials actually use,
//! plus a check on the value's own shape for the formats that are unmistakable.
//! It is deliberately eager: a false positive costs a field the model probably
//! did not need, and a false negative costs a credential.
//!
//! # What gives way under pressure
//!
//! 1. **Array samples**, shortest first — the element count is kept regardless,
//!    so "five thousand users" survives even when no user does.
//! 2. **Depth**, outermost last — a nested object becomes a named type, and the
//!    path that was elided is listed.
//! 3. **The sample entirely**, leaving the schema.
//!
//! The counts and the elided paths are what stop this from being lossy in the
//! dangerous sense: the model can always see *that* there was more.

use async_trait::async_trait;
use boundgraph_core::{Digest, SchemaId, SlicerId};
use serde_json::{Map, Value, json};

use crate::slicer::{Raw, Slice, SliceError, Slicer};

/// The schema a JSON condensate declares.
pub const JSON_SCHEMA: &str = "boundgraph.json.v1";

/// What a redacted value is replaced with.
pub const REDACTED: &str = "<redacted>";

/// Field names that carry credentials, by convention.
///
/// Matched as substrings of the lowercased key, because the wrapping varies
/// endlessly — `api_key`, `apiKey`, `X-Api-Key`, `client_secret` — while the
/// stem does not.
const SECRET_KEYS: &[&str] = &[
    "password",
    "passwd",
    "secret",
    "token",
    "api_key",
    "apikey",
    "access_key",
    "accesskey",
    "private_key",
    "privatekey",
    "authorization",
    "auth",
    "credential",
    "session_id",
    "sessionid",
    "cookie",
    "signature",
    "client_secret",
    "refresh",
    "bearer",
    "otp",
    "pin",
];

/// Whether a field name suggests a credential.
pub fn is_secret_key(key: &str) -> bool {
    let lower = key.to_ascii_lowercase().replace('-', "_");
    SECRET_KEYS.iter().any(|k| lower.contains(k))
}

/// Whether a *value* is unmistakably a credential whatever it is called.
///
/// Only the formats that cannot plausibly be anything else. A long hex string
/// is a digest as often as it is a key, so length alone is not enough — that
/// would redact every content address in the system.
pub fn looks_like_secret(value: &str) -> bool {
    // Vendor-prefixed keys: `sk-…`, `ghp_…`, `AKIA…`, `xoxb-…`.
    const PREFIXES: &[&str] = &[
        "sk-",
        "sk_live_",
        "sk_test_",
        "pk_live_",
        "ghp_",
        "gho_",
        "ghs_",
        "github_pat_",
        "xoxb-",
        "xoxp-",
        "AKIA",
        "ASIA",
        "AIza",
        "-----BEGIN",
    ];
    if PREFIXES.iter().any(|p| value.starts_with(p)) {
        return true;
    }

    // A JWT: three base64url segments separated by dots, with a `eyJ` header.
    if value.starts_with("eyJ") && value.matches('.').count() == 2 {
        return true;
    }
    false
}

/// A schema inferred from a value.
///
/// Deliberately a small subset of JSON Schema — types, properties, and item
/// shapes. Enough for a model to plan against and for a consumer to compare
/// two responses; not an attempt to reconstruct a contract the API never
/// published. Validating against a *declared* contract is US-0803.
pub fn infer(value: &Value) -> Value {
    match value {
        Value::Null => json!({ "type": "null" }),
        Value::Bool(_) => json!({ "type": "boolean" }),
        Value::Number(n) => {
            if n.is_i64() || n.is_u64() {
                json!({ "type": "integer" })
            } else {
                json!({ "type": "number" })
            }
        }
        Value::String(_) => json!({ "type": "string" }),
        Value::Array(items) => {
            let mut schema = json!({ "type": "array" });
            if let Some(merged) = items.iter().map(infer).reduce(|a, b| merge(&a, &b)) {
                schema["items"] = merged;
            }
            schema
        }
        Value::Object(map) => {
            let mut properties = Map::new();
            for (key, child) in map {
                // The key survives even when its value will not: knowing a
                // response carries a `token` field is shape, not secret.
                properties.insert(key.clone(), infer(child));
            }
            json!({ "type": "object", "properties": Value::Object(properties) })
        }
    }
}

/// Combine two inferred schemas.
///
/// Arrays in real responses are homogeneous but not perfectly so — a nullable
/// field is present on some elements and null on others. Merging across every
/// element rather than trusting the first is what stops the schema from
/// claiming a field is required when it is not.
fn merge(a: &Value, b: &Value) -> Value {
    if a == b {
        return a.clone();
    }
    let (ta, tb) = (a["type"].as_str(), b["type"].as_str());

    // A nullable field: report the type it has when it has one.
    if ta == Some("null") {
        return b.clone();
    }
    if tb == Some("null") {
        return a.clone();
    }

    if ta == Some("object") && tb == Some("object") {
        let mut properties = a["properties"].as_object().cloned().unwrap_or_default();
        if let Some(other) = b["properties"].as_object() {
            for (key, schema) in other {
                let merged = match properties.get(key) {
                    Some(existing) => merge(existing, schema),
                    None => schema.clone(),
                };
                properties.insert(key.clone(), merged);
            }
        }
        return json!({ "type": "object", "properties": Value::Object(properties) });
    }

    if ta == Some("array") && tb == Some("array") {
        let mut schema = json!({ "type": "array" });
        match (a.get("items"), b.get("items")) {
            (Some(x), Some(y)) => schema["items"] = merge(x, y),
            (Some(x), None) => schema["items"] = x.clone(),
            (None, Some(y)) => schema["items"] = y.clone(),
            (None, None) => {}
        }
        return schema;
    }

    // Genuinely mixed. Saying so beats picking one and being wrong.
    json!({ "type": [ta.unwrap_or("unknown"), tb.unwrap_or("unknown")] })
}

/// Condenses JSON responses.
///
/// # Example
///
/// ```
/// use boundgraph_slice::{JsonSlicer, MemoryStore, Raw, Slicer};
///
/// let body = serde_json::json!({
///     "users": (0..5_000).map(|i| serde_json::json!({ "id": i })).collect::<Vec<_>>(),
///     "api_key": "sk-live-do-not-leak-this",
/// });
///
/// let condensate = tokio::runtime::Builder::new_current_thread()
///     .build()?
///     .block_on(JsonSlicer::new().condense(
///         &Raw::new(body.to_string()),
///         300,
///         &MemoryStore::new(),
///     ))?;
///
/// let payload = format!("{:?}", condensate.payload);
/// assert!(!payload.contains("do-not-leak-this"), "the key never reaches the model");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone)]
pub struct JsonSlicer {
    max_depth: usize,
    array_sample: usize,
    extra_secret_keys: Vec<String>,
}

impl Default for JsonSlicer {
    fn default() -> Self {
        Self::new()
    }
}

impl JsonSlicer {
    /// A slicer sampling three array elements and four levels deep.
    pub fn new() -> Self {
        Self {
            max_depth: 4,
            array_sample: 3,
            extra_secret_keys: Vec::new(),
        }
    }

    /// Sample this many array elements, and elide below this depth.
    #[must_use]
    pub fn sampling(mut self, elements: usize, max_depth: usize) -> Self {
        self.array_sample = elements;
        self.max_depth = max_depth.max(1);
        self
    }

    /// Treat these field names as secret too, on top of the built-in list.
    ///
    /// For a response whose credential field is called something nobody would
    /// guess. The built-in list is not replaced — narrowing it is not offered,
    /// because the cost of the two mistakes is not symmetric.
    #[must_use]
    pub fn redacting<S: Into<String>>(mut self, keys: impl IntoIterator<Item = S>) -> Self {
        self.extra_secret_keys
            .extend(keys.into_iter().map(|k| k.into().to_ascii_lowercase()));
        self
    }

    fn is_secret(&self, key: &str) -> bool {
        if is_secret_key(key) {
            return true;
        }
        let lower = key.to_ascii_lowercase();
        self.extra_secret_keys.iter().any(|k| lower.contains(k))
    }

    /// Reduce a value to a sample, redacting and recording what was elided.
    fn sample(
        &self,
        value: &Value,
        depth: usize,
        elements: usize,
        path: &str,
        elided: &mut Vec<String>,
        counts: &mut Map<String, Value>,
    ) -> Value {
        match value {
            Value::Array(items) => {
                if !items.is_empty() {
                    // Kept whatever else is dropped: "five thousand users"
                    // survives even when no user does.
                    counts.insert(path.to_string(), json!(items.len()));
                }
                if depth == 0 {
                    elided.push(path.to_string());
                    return json!(format!("<array of {}>", items.len()));
                }
                let taken: Vec<Value> = items
                    .iter()
                    .take(elements)
                    .enumerate()
                    .map(|(i, item)| {
                        self.sample(
                            item,
                            depth - 1,
                            elements,
                            &format!("{path}[{i}]"),
                            elided,
                            counts,
                        )
                    })
                    .collect();
                if taken.len() < items.len() {
                    elided.push(format!("{path}[]"));
                }
                Value::Array(taken)
            }
            Value::Object(map) => {
                if depth == 0 {
                    elided.push(path.to_string());
                    return json!(format!("<object with {} fields>", map.len()));
                }
                let mut out = Map::new();
                for (key, child) in map {
                    // Redaction is not subject to depth, sampling, or the cap.
                    if self.is_secret(key) {
                        out.insert(key.clone(), json!(REDACTED));
                        continue;
                    }
                    if let Value::String(text) = child
                        && looks_like_secret(text)
                    {
                        out.insert(key.clone(), json!(REDACTED));
                        continue;
                    }
                    out.insert(
                        key.clone(),
                        self.sample(
                            child,
                            depth - 1,
                            elements,
                            &format!("{path}.{key}"),
                            elided,
                            counts,
                        ),
                    );
                }
                Value::Object(out)
            }
            Value::String(text) if looks_like_secret(text) => json!(REDACTED),
            other => other.clone(),
        }
    }

    /// The payload at a given fidelity. `None` for `elements` means no sample.
    fn payload(&self, value: &Value, schema: &Value, fidelity: Option<(usize, usize)>) -> Value {
        let mut payload = json!({
            "schema": schema,
            // A stable fingerprint of the shape, so a consumer can tell that
            // two responses are the same kind of thing without diffing them.
            "shape": format!("b3:{}", &Digest::of(schema.to_string().as_bytes()).to_string()[..16]),
        });

        let Some((depth, elements)) = fidelity else {
            return payload;
        };

        let mut elided = Vec::new();
        let mut counts = Map::new();
        let sample = self.sample(value, depth, elements, "$", &mut elided, &mut counts);

        payload["sample"] = sample;
        if !counts.is_empty() {
            payload["counts"] = Value::Object(counts);
        }
        if !elided.is_empty() {
            elided.sort();
            elided.dedup();
            payload["elided"] = json!(elided);
        }
        payload
    }
}

#[async_trait]
impl Slicer for JsonSlicer {
    fn id(&self) -> SlicerId {
        SlicerId::new("json", 1)
    }

    async fn slice(&self, raw: &Raw, cap: usize) -> Result<Slice, SliceError> {
        let text = raw.text();
        let value: Value = serde_json::from_str(&text).map_err(|e| SliceError::Unsuitable {
            slicer: "json".to_string(),
            detail: format!("not JSON: {e}"),
        })?;

        let schema = infer(&value);
        let out_schema = SchemaId::new(JSON_SCHEMA, 1);
        let counter = self.counter();
        let fits = |v: &Value| counter.count(&serde_json::to_string(v).unwrap_or_default()) <= cap;

        // The schema is retained unconditionally, so if it alone does not fit
        // there is nothing to produce.
        let floor = self.payload(&value, &schema, None);
        if !fits(&floor) {
            return Err(SliceError::CapTooSmall {
                cap,
                minimum: counter.count(&serde_json::to_string(&floor).unwrap_or_default()),
            });
        }

        // Fidelity ladder: shorten array samples first, then reduce depth.
        for depth in (1..=self.max_depth).rev() {
            for elements in (0..=self.array_sample).rev() {
                let candidate = self.payload(&value, &schema, Some((depth, elements)));
                if fits(&candidate) {
                    let lossy = candidate.get("elided").is_some()
                        || candidate["sample"] != value
                        || depth < self.max_depth
                        || elements < self.array_sample;
                    return Ok(Slice {
                        schema: out_schema,
                        payload: candidate,
                        lossy,
                    });
                }
            }
        }

        Ok(Slice {
            schema: out_schema,
            payload: floor,
            lossy: true,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::slicer::MemoryStore;

    fn raw_of(value: &Value) -> Raw {
        Raw::new(value.to_string())
    }

    async fn slice(value: &Value, cap: usize) -> Slice {
        JsonSlicer::new()
            .slice(&raw_of(value), cap)
            .await
            .expect("a satisfiable cap")
    }

    fn big_response() -> Value {
        json!({
            "users": (0..5_000)
                .map(|i| json!({ "id": i, "email": format!("user{i}@example.test"), "admin": false }))
                .collect::<Vec<_>>(),
            "next_cursor": "abc123",
            "total": 5_000,
        })
    }

    fn rendered(slice: &Slice) -> String {
        serde_json::to_string(&slice.payload).unwrap()
    }

    // ── the schema always survives ──────────────────────────────────────────

    /// The criterion, and the reason the slicer exists.
    #[tokio::test]
    async fn the_schema_survives_a_cap_that_fits_almost_nothing() {
        for cap in [100, 120, 200, 400, 2_000] {
            let slice = slice(&big_response(), cap).await;
            let schema = &slice.payload["schema"];

            assert_eq!(schema["type"], "object", "at cap {cap}");
            assert_eq!(
                schema["properties"]["users"]["items"]["properties"]["email"]["type"], "string",
                "the nested element shape survives cap {cap}"
            );
            assert_eq!(schema["properties"]["next_cursor"]["type"], "string");
        }
    }

    #[test]
    fn scalars_infer_their_own_types() {
        assert_eq!(infer(&json!(null))["type"], "null");
        assert_eq!(infer(&json!(true))["type"], "boolean");
        assert_eq!(infer(&json!(42))["type"], "integer");
        assert_eq!(infer(&json!(1.5))["type"], "number");
        assert_eq!(infer(&json!("x"))["type"], "string");
    }

    /// Trusting the first element would claim a field is always present when a
    /// later element omits it.
    #[test]
    fn an_array_schema_merges_every_element_rather_than_trusting_the_first() {
        let value = json!([{ "a": 1 }, { "b": "two" }]);
        let items = &infer(&value)["items"]["properties"];

        assert_eq!(items["a"]["type"], "integer");
        assert_eq!(items["b"]["type"], "string");
    }

    #[test]
    fn a_nullable_field_reports_the_type_it_has_when_it_has_one() {
        let value = json!([{ "cursor": null }, { "cursor": "abc" }]);
        assert_eq!(
            infer(&value)["items"]["properties"]["cursor"]["type"],
            "string"
        );
    }

    #[test]
    fn a_genuinely_mixed_field_says_so_rather_than_picking_one() {
        let value = json!([{ "x": 1 }, { "x": "one" }]);
        let x = &infer(&value)["items"]["properties"]["x"]["type"];
        assert!(x.is_array(), "expected a union, got {x}");
    }

    /// The criterion: a schema id so consumers can compare and validate.
    #[tokio::test]
    async fn two_responses_of_the_same_shape_share_a_fingerprint() {
        let a = json!({ "users": [{ "id": 1 }], "total": 1 });
        let b = json!({ "users": [{ "id": 999 }], "total": 999 });
        let c = json!({ "users": [{ "id": 1, "extra": true }], "total": 1 });

        async fn shape(value: &Value) -> Value {
            slice(value, 500).await.payload["shape"].clone()
        }

        assert_eq!(shape(&a).await, shape(&b).await, "same shape, other values");
        assert_ne!(
            shape(&a).await,
            shape(&c).await,
            "an extra field is a different shape"
        );
    }

    #[tokio::test]
    async fn the_condensate_declares_its_schema_id() {
        let store = MemoryStore::new();
        let condensate = JsonSlicer::new()
            .condense(&raw_of(&big_response()), 500, &store)
            .await
            .unwrap();

        assert_eq!(condensate.schema, SchemaId::new(JSON_SCHEMA, 1));
        assert_eq!(condensate.slicer, SlicerId::new("json", 1));
    }

    // ── arrays ──────────────────────────────────────────────────────────────

    /// The criterion: a retained element count and a sample.
    #[tokio::test]
    async fn a_long_array_keeps_its_count_and_a_sample() {
        let slice = slice(&big_response(), 800).await;

        assert_eq!(
            slice.payload["counts"]["$.users"], 5_000,
            "the count survives whatever the sample"
        );
        let sample = slice.payload["sample"]["users"].as_array();
        if let Some(sample) = sample {
            assert!(sample.len() < 5_000, "it is a sample, not the array");
        }
        assert!(slice.lossy);
    }

    /// The count is the part that must not be lost: "five thousand users"
    /// survives even when no user does.
    #[tokio::test]
    async fn the_element_count_survives_even_when_no_element_does() {
        let slice = slice(&big_response(), 200).await;
        assert_eq!(slice.payload["counts"]["$.users"], 5_000);
    }

    #[tokio::test]
    async fn a_short_array_is_kept_whole_and_is_not_lossy() {
        let value = json!({ "items": [1, 2] });
        let slice = slice(&value, 400).await;

        assert_eq!(slice.payload["sample"], value);
        assert!(!slice.lossy);
        assert!(slice.payload.get("elided").is_none());
    }

    #[tokio::test]
    async fn the_sample_width_is_configurable() {
        let value = json!({ "xs": (0..50).collect::<Vec<_>>() });
        let wide = JsonSlicer::new()
            .sampling(10, 4)
            .slice(&raw_of(&value), 2_000)
            .await
            .unwrap();

        assert_eq!(wide.payload["sample"]["xs"].as_array().unwrap().len(), 10);
    }

    // ── depth ───────────────────────────────────────────────────────────────

    /// The criterion: elided by depth, with the paths named.
    #[tokio::test]
    async fn deep_nesting_is_elided_and_the_paths_are_named() {
        let deep = json!({ "a": { "b": { "c": { "d": { "e": { "f": 1 } } } } } });
        let slice = JsonSlicer::new()
            .sampling(3, 2)
            .slice(&raw_of(&deep), 400)
            .await
            .unwrap();

        let elided = slice.payload["elided"].as_array().unwrap();
        assert!(
            elided.iter().any(|p| p.as_str().unwrap().contains("$.a.b")),
            "the elided path should be named: {elided:?}"
        );
        assert!(rendered(&slice).contains("<object with"));
        assert!(slice.lossy);
    }

    #[tokio::test]
    async fn an_elided_object_says_how_many_fields_it_had() {
        let value = json!({ "outer": { "a": 1, "b": 2, "c": 3 } });
        let slice = JsonSlicer::new()
            .sampling(3, 1)
            .slice(&raw_of(&value), 400)
            .await
            .unwrap();

        assert_eq!(slice.payload["sample"]["outer"], "<object with 3 fields>");
    }

    // ── secrets ─────────────────────────────────────────────────────────────

    /// The criterion, and the one where a false negative is expensive.
    #[tokio::test]
    async fn secret_looking_fields_are_redacted_rather_than_sampled() {
        let value = json!({
            "user": "alice",
            "password": "hunter2",
            "api_key": "kx-1234",
            "apiKey": "kx-5678",
            "X-Api-Key": "kx-9012",
            "access_token": "at-abcd",
            "refresh_token": "rt-efgh",
            "client_secret": "cs-ijkl",
            "authorization": "Basic dXNlcjpwYXNz",
            "session_id": "sess-mnop",
            "signature": "sig-qrst",
        });

        let slice = slice(&value, 2_000).await;
        let text = rendered(&slice);

        for leaked in [
            "hunter2",
            "kx-1234",
            "kx-5678",
            "kx-9012",
            "at-abcd",
            "rt-efgh",
            "cs-ijkl",
            "dXNlcjpwYXNz",
            "sess-mnop",
            "sig-qrst",
        ] {
            assert!(
                !text.contains(leaked),
                "{leaked} reached the payload: {text}"
            );
        }
        assert_eq!(
            slice.payload["sample"]["user"], "alice",
            "and the rest survives"
        );
    }

    /// A token that fits is worse than one that does not — it reaches the
    /// model, the ledger, and every sink the ledger feeds.
    #[tokio::test]
    async fn redaction_happens_regardless_of_the_cap() {
        let value = json!({ "api_key": "sk-live-secret-value" });
        for cap in [40, 80, 200, 5_000] {
            let slice = slice(&value, cap).await;
            assert!(
                !rendered(&slice).contains("secret-value"),
                "leaked at cap {cap}"
            );
        }
    }

    /// The shape is not the secret: knowing a response carries a `token` field
    /// is exactly what a model needs to plan, and it is not a disclosure.
    #[tokio::test]
    async fn the_key_survives_even_though_its_value_does_not() {
        let slice = slice(&json!({ "api_key": "sk-live-abc" }), 400).await;

        assert_eq!(
            slice.payload["schema"]["properties"]["api_key"]["type"],
            "string"
        );
        assert_eq!(slice.payload["sample"]["api_key"], REDACTED);
    }

    /// Some values are unmistakable whatever the field is called.
    #[tokio::test]
    async fn a_credential_shaped_value_is_redacted_under_an_innocent_name() {
        let value = json!({
            "note": "sk-live-51H8yZ",
            "header": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIn0.abc",
            "key_material": "-----BEGIN RSA PRIVATE KEY-----",
            "aws": "AKIAIOSFODNN7EXAMPLE",
        });

        let slice = slice(&value, 2_000).await;
        let text = rendered(&slice);
        for leaked in ["51H8yZ", "eyJhbGci", "BEGIN RSA", "AKIAIOSFODNN7EXAMPLE"] {
            assert!(!text.contains(leaked), "{leaked} survived: {text}");
        }
    }

    /// A digest is a long hex string and so is an API key. Redacting on length
    /// alone would redact every content address in the system.
    #[tokio::test]
    async fn an_ordinary_long_string_is_not_mistaken_for_a_credential() {
        let digest = "a".repeat(64);
        let value = json!({ "digest": digest, "message": "a fairly long ordinary sentence" });
        let slice = slice(&value, 2_000).await;

        assert_eq!(slice.payload["sample"]["digest"], digest);
        assert!(rendered(&slice).contains("ordinary sentence"));
    }

    #[tokio::test]
    async fn extra_secret_names_can_be_declared() {
        let value = json!({ "house_key": "front-door-code" });

        let default = slice(&value, 400).await;
        assert_eq!(default.payload["sample"]["house_key"], "front-door-code");

        let strict = JsonSlicer::new()
            .redacting(["house_key"])
            .slice(&raw_of(&value), 400)
            .await
            .unwrap();
        assert_eq!(strict.payload["sample"]["house_key"], REDACTED);
    }

    #[test]
    fn the_key_conventions_are_matched_however_they_are_wrapped() {
        for key in [
            "password",
            "Password",
            "api_key",
            "apiKey",
            "X-API-KEY",
            "client_secret",
            "refresh_token",
            "AUTHORIZATION",
            "sessionId",
            "Cookie",
        ] {
            assert!(is_secret_key(key), "{key} should be secret");
        }
        for key in ["username", "id", "email", "created_at", "digest", "count"] {
            assert!(!is_secret_key(key), "{key} should not be");
        }
    }

    // ── the contract ────────────────────────────────────────────────────────

    #[tokio::test]
    async fn the_output_never_exceeds_the_cap() {
        let store = MemoryStore::new();
        let raw = raw_of(&big_response());

        for cap in [100, 120, 200, 400, 800, 2_000, 8_000] {
            let condensate = JsonSlicer::new()
                .condense(&raw, cap, &store)
                .await
                .unwrap_or_else(|e| panic!("cap {cap} should be satisfiable: {e}"));
            assert_eq!(condensate.cap, cap);
        }
    }

    #[tokio::test]
    async fn slicing_is_deterministic() {
        let raw = raw_of(&big_response());
        let first = JsonSlicer::new().slice(&raw, 600).await.unwrap();
        for _ in 0..8 {
            assert_eq!(JsonSlicer::new().slice(&raw, 600).await.unwrap(), first);
        }
    }

    /// Not JSON is a fallback-able condition, not a failure of the step — the
    /// caller can try the log slicer, exactly as the AST slicer does.
    #[tokio::test]
    async fn input_that_is_not_json_is_unsuitable_rather_than_fatal() {
        let err = JsonSlicer::new()
            .slice(&Raw::new("this is not json at all"), 400)
            .await
            .unwrap_err();

        assert!(matches!(err, SliceError::Unsuitable { .. }));
        assert!(err.can_fall_back());
    }

    #[tokio::test]
    async fn a_response_that_fits_entirely_is_not_marked_lossy() {
        let value = json!({ "ok": true, "count": 2 });
        let slice = slice(&value, 400).await;

        assert!(!slice.lossy);
        assert_eq!(slice.payload["sample"], value);
    }

    #[tokio::test]
    async fn a_top_level_array_is_handled_as_readily_as_an_object() {
        let value = json!((0..100).map(|i| json!({ "i": i })).collect::<Vec<_>>());
        let slice = slice(&value, 400).await;

        assert_eq!(slice.payload["schema"]["type"], "array");
        assert_eq!(slice.payload["counts"]["$"], 100);
    }
}