supercode-reduce 0.5.46

Optional lossless, reversible session reduction for Volter Harness
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
//! T12 — model-invocable rehydration/retrieval over the sidecar (SPEC.md
//! TR-1): the two agent intrinsics `expand_reduction` and `sidecar_search`.
//! "The missing half of A7": until this landed, only the USER could recover
//! reduced content mid-flight (C4 `/expand`); the model itself had no way to
//! ask for it back, which is exactly what capped how aggressive every other
//! reducer could safely be.
//!
//! # The two resolution sources
//!
//! Both functions here are pure resolvers — no filesystem I/O, no
//! no session container required — over TWO canonical message slices:
//!
//! - `minted_view`: the messages the reductions in `log` were minted against
//!   (a live agent passes `history[1..]`; offline callers
//!   pass a loaded `Session`'s `.messages`). Every `SidecarPtr` hash is
//!   verified against THIS slice, because this is the slice
//!   [`super::project_messages`] hashed when it created the reduction.
//! - `recorded`: optionally, the recorder's full-fidelity recorded messages
//!   (the sidecar reloaded from disk). **Defense in depth, since TR-12
//!   (SPEC.md D6/A7 supersession, `Agent::run_loop`):** for any session
//!   recorded under that gate — a recorder and a [`super::ReductionPolicy`]
//!   both installed, which is what actually triggers minting a reduction over
//!   `history` in the first place — `Agent::cap_tool_output` is off, so
//!   `minted_view` (`history[1..]`) already holds the same full bytes as
//!   `recorded`, and every upgrade below is a provable no-op (`rc != minted`
//!   fails, `minted` returned unchanged). The path still matters for two
//!   cases where `history`/the sidecar genuinely can diverge: (1) a
//!   **legacy** sidecar recorded before this gate existed, whose reductions
//!   were minted from an already-`cap_tool_output`-capped `history` (`minted_view`'s
//!   copy is a capped prefix + an honest cap notice, and the ONLY place the
//!   full bytes still exist is the recorded copy); (2) a policy-without-recorder
//!   agent (gate off because nothing durable backs the full bytes) that later
//!   gains a recorder. In either case, when `recorded` is provided and its
//!   copy of an addressed message matches the full supersession key — same
//!   index, same role, same `tool_call_id` (present on BOTH sides; only tool
//!   results are ever capped, and the id is unique per call), and the minted
//!   copy is a cap-notice-bearing prefix of the recorded copy — the recorded
//!   bytes are returned instead. The `tool_call_id` component is what makes
//!   the key exact rather than heuristic: after `Agent::rewind_to` truncates
//!   history (the sidecar is append-only), a re-run command can produce a
//!   same-index, same-role tool result sharing the old run's entire kept
//!   prefix, but it always carries a fresh `tool_call_id`, so the
//!   old-timeline copy can never satisfy the key. Anything else falls back
//!   to the hash-verified minted copy, so a wrong-bytes substitution is
//!   structurally impossible: the result is always either the exact bytes
//!   the hash was minted from, or their verified same-call full-length
//!   superset.
//!
//! # No new reductions, no sentinel text
//!
//! This module **deliberately mints no new [`Reduction`] records and never
//! writes [`REDUCTION_SENTINEL`] text.** An oversized [`expand_reduction`]
//! result is just an ordinary tool result once the agent loop pushes it onto
//! history — the EXISTING [`super::project_messages`] A7 pass re-truncates
//! it (with a fresh, genuinely reversible stub, recorded in the log like any
//! other reduction) once it ages out of
//! `ReductionPolicy::protect_last_n_tool_results` on a later turn. That is
//! what SPEC.md TR-1 dev/05 ("expand results are reduction-eligible") tests,
//! and it is also why TR-1's "no leak-guard special case needed" holds:
//! nothing in this module ever produces sentinel-bearing text, so a session
//! that used these intrinsics exports through `export_session`'s existing,
//! unconditional leak guard (A11) unmodified. `byte_range` is the proactive
//! tool: a caller who slices a large reduction into sub-`ReductionPolicy`-
//! cap chunks never needs the safety net at all.
//!
//! Note that expanding does NOT remove the reduction from the log — the
//! stub stays in the projected view (unchanged), and the expanded content
//! arrives as a new tool result. That is correct and deliberate: the log
//! entry must survive so the stub keeps resolving (for a later re-expand,
//! for `sidecar_search`, and for C4's offline tooling); contrast
//! [`super::invert_one_messages`], which really does splice the original back into a
//! view and therefore removes the entry.
//!
//! [`REDUCTION_SENTINEL`]: super::REDUCTION_SENTINEL

use serde::{Deserialize, Serialize};

use super::stub::Kind;
use super::{
    char_boundary_floor, resolve_image_part, resolve_original_content, resolve_tool_input_value,
    resolve_turns_range, Reduction, ReductionKind, ReductionLog,
};
use crate::{ReductionError as Error, Result};
use supercode_interchange::ChatMessage;

/// Marker appended to a retained prefix when a runtime caps tool output.
///
/// This is owned by the reduction/rehydration contract because rehydration
/// must recognize capped copies without depending on any particular agent
/// runtime implementation. Runtime packages consume this marker when they
/// create a capped view.
pub const CAP_NOTICE_MARKER: &str = "\n\n[supercode: tool output truncated — ";

/// The result of a successful [`expand_reduction`] call.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExpandOutcome {
    /// The resolved (optionally range-sliced) original content — exactly
    /// what was asked for, byte for byte. This never truncates further on
    /// its own; see the module doc comment for why an oversized ask is left
    /// to the ordinary A7 pass instead, once this content lands in history.
    pub content: String,
    /// Total byte length of the FULL original content behind the requested
    /// id, before any `byte_range` slicing — lets a caller judge whether (and
    /// how) to ask for a narrower range next time.
    pub total_bytes: usize,
    /// The effective `[start, end)` byte range `content` was sliced to, when
    /// the caller asked for one (clamped to `total_bytes` and to char
    /// boundaries). `None` for a whole-content expand.
    pub range: Option<(usize, usize)>,
}

/// Resolve the `expand_reduction(reduction_id, byte_range?)` agent intrinsic
/// (SPEC.md TR-1 dev/01): the exact original bytes behind reduction `id` in
/// `log`, resolved against `minted_view` (hash-verified) with `recorded`
/// preferred for cap-diverged content — see the module doc comment for the
/// two-source contract. With `byte_range = Some((start, end))`, just that
/// `[start, end)` slice (end clamped to `total_bytes`, char-boundary-safe).
///
/// Errors, all model-recoverable:
/// - unknown `id` → names every currently-valid id (mirrors C4's `/expand`
///   error style);
/// - reversed range (`start > end`) or `start` beyond the original's total
///   bytes → names the expected `[start, end)` form and the true
///   `total_bytes`, rather than ever silently returning empty or full
///   content the caller didn't ask for.
pub fn expand_reduction(
    log: &ReductionLog,
    minted_view: &[ChatMessage],
    recorded: Option<&[ChatMessage]>,
    id: &str,
    byte_range: Option<(usize, usize)>,
) -> Result<ExpandOutcome> {
    let r = find_reduction(log, id)?;
    let text = resolve_text(r, minted_view, recorded)?;
    let total_bytes = text.len();
    let range = match byte_range {
        Some((s, e)) => {
            if s > e {
                return Err(Error::new(format!(
                    "expand_reduction: reversed byte_range [{s}, {e}) — expected [start, end) \
                     with start <= end; the original is {total_bytes} bytes"
                )));
            }
            if s > total_bytes {
                return Err(Error::new(format!(
                    "expand_reduction: byte_range start {s} is beyond the original's \
                     {total_bytes} bytes — expected [start, end) with start <= {total_bytes}"
                )));
            }
            let cs = char_boundary_floor(&text, s);
            let ce = char_boundary_floor(&text, e.min(total_bytes)).max(cs);
            Some((cs, ce))
        }
        None => None,
    };
    let (start, end) = range.unwrap_or((0, total_bytes));
    Ok(ExpandOutcome {
        content: text[start..end].to_string(),
        total_bytes,
        range,
    })
}

/// Total byte length of the original content behind reduction `id` — what
/// [`ExpandOutcome::total_bytes`] would report — without slicing anything.
/// Used by the agent's argument-validation error path so a malformed
/// `byte_range` error can name the true size the caller is ranging over.
pub fn reduction_total_bytes(
    log: &ReductionLog,
    minted_view: &[ChatMessage],
    recorded: Option<&[ChatMessage]>,
    id: &str,
) -> Result<usize> {
    let r = find_reduction(log, id)?;
    Ok(resolve_text(r, minted_view, recorded)?.len())
}

/// One match [`sidecar_search`] found.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SidecarSearchMatch {
    /// The reduction whose hidden content this match was found in — pass
    /// this straight to [`expand_reduction`] to see more of it.
    pub reduction_id: String,
    /// The stub `<kind>` token (SPEC.md D2/C2 grammar), e.g. `"tool-output"`.
    /// Owned (rather than `&'static str`, though [`Kind::as_str`] always
    /// hands back one) so this type round-trips through
    /// `serde_json::from_str` — a derived `Deserialize` for a `&'static str`
    /// field would need the deserializer's whole input to live for
    /// `'static`, which a freshly-parsed tool-result string never does.
    pub kind: String,
    /// A short window of text around the match — not the whole hidden span.
    pub snippet: String,
}

/// The complete result of a [`sidecar_search`] call. Bounded by construction
/// (SPEC.md TR-1 fix pass): at most `MAX_MATCHES` snippets, each at most
/// `SNIPPET_MAX_BYTES`, and the whole serialized form at most
/// `MAX_RESULT_BYTES` — a broad query over megabytes of hidden content can
/// never blow the result back up to the size the reductions saved, and the
/// truncation is honest, structured JSON (never a mid-structure byte chop).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SidecarSearchResult {
    /// The matches kept (first `MAX_MATCHES` found, then byte-capped).
    pub matches: Vec<SidecarSearchMatch>,
    /// How many matches the query REALLY had, including ones dropped by the
    /// caps — `truncated && total_matches > matches.len()` tells the model
    /// to narrow its query.
    pub total_matches: usize,
    /// True when any match was dropped by `MAX_MATCHES`/`MAX_RESULT_BYTES`.
    pub truncated: bool,
    /// Reductions in the log whose content could not currently be resolved
    /// (e.g. their address no longer exists after `Agent::rewind_to`). They
    /// were skipped, not fatal — one stale log entry never disables search
    /// over the rest.
    pub unresolvable: usize,
}

/// Bytes of context kept on each side of a match for [`SidecarSearchMatch::snippet`].
const SNIPPET_RADIUS: usize = 80;

/// Hard cap on the byte length of one [`SidecarSearchMatch::snippet`] (a
/// regex like `z.*` can match almost an entire hidden span; the snippet must
/// stay a snippet).
const SNIPPET_MAX_BYTES: usize = 200;

/// Hard cap on how many matches a single [`sidecar_search`] returns.
const MAX_MATCHES: usize = 50;

/// Hard cap on the serialized byte length of a [`SidecarSearchResult`].
const MAX_RESULT_BYTES: usize = 65_536;

/// Resolve the `sidecar_search(query)` agent intrinsic (SPEC.md TR-1
/// dev/04): substring/regex search over content CURRENTLY reduced out of the
/// view — every [`Reduction`] in `log`, restricted to its `hidden_span` so
/// a still-visible portion (e.g. `ToolOutputTruncated`'s kept prefix) is
/// never matched. "The same string visible in the live view is not
/// double-reported" is upheld entirely from the log's own records — this
/// still never inspects the live view: for most kinds the hidden span is a
/// pure function of the reduction itself, and for
/// [`ReductionKind::DuplicateOutput`] (TR-2, whose content is byte-identical
/// to a canonical instance that is usually still fully visible) visibility
/// is decided by whether any OTHER log record reduces the canonical's
/// address — no per-message record there and no enclosing `TurnsCleared`
/// range means the canonical is fully visible, so the duplicate's content is
/// not hidden and search skips it (`hidden_span` returns `None`). See the
/// module doc comment for the `minted_view`/`recorded` two-source contract.
///
/// An empty (or all-whitespace) `query` is an error, not a match-everything
/// wildcard: the empty pattern compiles as a regex that matches at every
/// position, which is never what a caller meant and used to make the result
/// size explode.
///
/// `query` is tried as a case-insensitive regex first; a query that fails to
/// compile as one (e.g. literal text with an unbalanced `[`/`(`, common in
/// file paths or code) falls back to a plain case-insensitive substring
/// search, so both "search for this exact snippet" and "search with a
/// pattern" work without the caller ever needing to escape anything.
pub fn sidecar_search(
    log: &ReductionLog,
    minted_view: &[ChatMessage],
    recorded: Option<&[ChatMessage]>,
    query: &str,
) -> Result<SidecarSearchResult> {
    if query.trim().is_empty() {
        return Err(Error::new(
            "sidecar_search: `query` must be a non-empty substring or regex".to_string(),
        ));
    }
    let regex = regex::RegexBuilder::new(query)
        .case_insensitive(true)
        .build()
        .ok();
    let lower_query = query.to_ascii_lowercase();

    let mut matches = Vec::new();
    let mut total_matches = 0usize;
    let mut unresolvable = 0usize;
    for r in &log.reductions {
        // Skip (and count) anything unresolvable rather than aborting the
        // whole search — after a rewind, one stale log entry must not
        // disable the intrinsic for every other reduction.
        let Ok(text) = resolve_text(r, minted_view, recorded) else {
            unresolvable += 1;
            continue;
        };
        let Some((hs, he)) = hidden_span(r, &text, log) else {
            // ImageRedacted (not a meaningful text-search target), or a
            // DuplicateOutput whose canonical is still fully visible (the
            // model can already see these exact bytes in the live view).
            continue;
        };
        let hidden = &text[hs..he];
        let kind = Kind::from(&r.kind).as_str().to_string();

        // `to_ascii_lowercase` (unlike `to_lowercase`) never changes byte
        // length, so match indices stay valid against `hidden` unchanged —
        // needed since the substring fallback path locates matches in the
        // lowercased copy but slices snippets out of the original.
        let positions: Vec<(usize, usize)> = match &regex {
            Some(re) => re.find_iter(hidden).map(|m| (m.start(), m.end())).collect(),
            None => hidden
                .to_ascii_lowercase()
                .match_indices(&lower_query)
                .map(|(i, m)| (i, i + m.len()))
                .collect(),
        };

        for (start, end) in positions {
            total_matches += 1;
            if matches.len() < MAX_MATCHES {
                matches.push(SidecarSearchMatch {
                    reduction_id: r.id.clone(),
                    kind: kind.clone(),
                    snippet: snippet_around(hidden, start, end),
                });
            }
        }
    }

    let mut result = SidecarSearchResult {
        truncated: total_matches > matches.len(),
        matches,
        total_matches,
        unresolvable,
    };
    // Final serialized-size cap: drop trailing matches (valid JSON with an
    // honest `truncated` flag, never a mid-structure byte chop) until the
    // whole result fits. With the per-snippet and match-count caps above
    // this loop almost never runs, but it makes the bound unconditional.
    while serialized_len(&result) > MAX_RESULT_BYTES && !result.matches.is_empty() {
        result.matches.pop();
        result.truncated = true;
    }
    Ok(result)
}

fn serialized_len(result: &SidecarSearchResult) -> usize {
    serde_json::to_string(result).map(|s| s.len()).unwrap_or(0)
}

/// Look up a reduction by id in `log`, erroring with a valid-id hint
/// (mirrors `cli/src/main.rs`'s `/expand` error style) when it isn't there.
fn find_reduction<'a>(log: &'a ReductionLog, id: &str) -> Result<&'a Reduction> {
    log.reductions.iter().find(|r| r.id == id).ok_or_else(|| {
        let valid: Vec<&str> = log.reductions.iter().map(|r| r.id.as_str()).collect();
        Error::new(format!(
            "expand_reduction: no reduction with id `{id}` — valid ids: {}",
            if valid.is_empty() {
                "(none)".to_string()
            } else {
                valid.join(", ")
            }
        ))
    })
}

/// Substitute `recorded`'s copy of the message at `index` for the
/// already-hash-verified `minted` content — but ONLY when the full
/// supersession key matches: same `index`, same role, same `tool_call_id`
/// (both sides must carry one — only tool results are ever
/// `cap_tool_output`-capped, and the id is unique per call), and `minted` is
/// a cap-notice-bearing prefix of the recorded copy (see
/// [`CAP_NOTICE_MARKER`]).
///
/// `tool_call_id` is what makes the key exact rather than heuristic: after
/// `Agent::rewind_to` truncates history while the append-only sidecar keeps
/// the old timeline, a re-run command can land a tool result at the SAME
/// index with the SAME role whose output shares the entire kept prefix with
/// the old run — but it always carries a NEW `tool_call_id`, so the
/// old-timeline recorded copy can never satisfy the key. Identical content,
/// a missing id on either side, role mismatch, or any unrelated divergence
/// all fall back to `minted` — never a silent wrong-bytes substitution.
fn prefer_recorded(
    minted: String,
    minted_msg: &ChatMessage,
    index: usize,
    recorded: Option<&[ChatMessage]>,
) -> String {
    let Some(rec) = recorded else { return minted };
    let Some(msg) = rec.get(index) else {
        return minted;
    };
    if msg.role != minted_msg.role {
        return minted;
    }
    let (Some(minted_id), Some(recorded_id)) = (
        minted_msg.tool_call_id.as_deref(),
        msg.tool_call_id.as_deref(),
    ) else {
        return minted; // Only tool results are ever capped; both must carry an id.
    };
    if minted_id != recorded_id {
        return minted;
    }
    let Some(rc) = msg.content.as_deref() else {
        return minted;
    };
    if rc != minted && capped_prefix_of(&minted, rc) {
        rc.to_string()
    } else {
        minted
    }
}

/// Is `minted` a `cap_tool_output`-capped copy of `full`? True iff `minted`
/// carries the cap-notice marker and everything before that marker is a
/// proper prefix of `full` — the exact construction `cap_tool_output`
/// performs (kept prefix + notice), verified byte-for-byte against the
/// candidate full copy.
fn capped_prefix_of(minted: &str, full: &str) -> bool {
    let Some(pos) = minted.rfind(CAP_NOTICE_MARKER) else {
        return false;
    };
    full.len() > pos && full.as_bytes().starts_with(&minted.as_bytes()[..pos])
}

/// The full text form of a reduction's ORIGINAL content: hash-verified
/// against `minted_view` (the same resolve primitives `invert`/`invert_one`
/// use, A6), then upgraded per-message to `recorded`'s full bytes where the
/// minted copy is a verified capped prefix ([`prefer_recorded`]) — rendered
/// to a single string for every kind so [`expand_reduction`]/
/// [`sidecar_search`] can treat them uniformly.
///
/// [`ReductionKind::DuplicateOutput`] (TR-2) resolves through its OWN address
/// exactly like [`ReductionKind::ToolOutputTruncated`]/[`ReductionKind::FileReadElided`]
/// — never by chasing `canonical`. `minted_view` (the agent's own `history`,
/// per the module doc comment) always retains the full original bytes at
/// every index regardless of what any given turn's PROJECTED view showed, so
/// self-resolution works unconditionally, including when the canonical
/// instance has ITSELF since been truncated or cleared (dev/04) — that only
/// ever changes what sits at the canonical's own address, never this one.
///
/// [`ReductionKind::OutputNormalized`] (T30/TR-4) likewise resolves through
/// [`resolve_original_content`] — which, for this kind, means the RAW
/// pre-normalization capture (ANSI/CR redraws and all), never the
/// normalized/rendered text the live view shows: the sidecar only ever
/// stores the raw bytes (A3), so byte-exact restore falls out of the exact
/// same self-addressed pattern every other kind uses, no special-casing
/// needed here.
fn resolve_text(
    r: &Reduction,
    minted_view: &[ChatMessage],
    recorded: Option<&[ChatMessage]>,
) -> Result<String> {
    match &r.kind {
        ReductionKind::ToolOutputTruncated { .. }
        | ReductionKind::FileReadElided { .. }
        | ReductionKind::OutputNormalized { .. }
        | ReductionKind::FileReadDiffed { .. }
        | ReductionKind::DuplicateOutput { .. }
        | ReductionKind::Superseded { .. } => {
            let minted = resolve_original_content(&r.ptr, minted_view)?;
            // Indexing is safe: resolve_original_content just verified the
            // address (and role) against this very slice.
            let minted_msg = &minted_view[r.ptr.addr.index];
            Ok(prefer_recorded(
                minted,
                minted_msg,
                r.ptr.addr.index,
                recorded,
            ))
        }
        ReductionKind::ImageRedacted { part_index } => {
            // `cap_tool_output` never touches `content_parts`, so the minted
            // copy IS the full copy here — no recorded upgrade needed.
            let part = resolve_image_part(&r.ptr, *part_index, minted_view)?;
            // The redacted content part's original `image_url.url` (a
            // `data:` URL) IS the original bytes for expand purposes; fall
            // back to the part's raw JSON if the shape is ever unexpected.
            Ok(part
                .get("image_url")
                .and_then(|iu| iu.get("url"))
                .and_then(|u| u.as_str())
                .map(str::to_string)
                .unwrap_or_else(|| part.to_string()))
        }
        ReductionKind::TurnsCleared { first, last, .. } => {
            let msgs = resolve_turns_range(&r.ptr, *first, *last, minted_view)?;
            let enriched: Vec<ChatMessage> = msgs
                .into_iter()
                .enumerate()
                .map(|(offset, mut m)| {
                    if let Some(content) = m.content.take() {
                        let upgraded = prefer_recorded(content, &m, first + offset, recorded);
                        m.content = Some(upgraded);
                    }
                    m
                })
                .collect();
            Ok(render_turns(&enriched))
        }
        ReductionKind::ToolInputElided { call_id, field, .. } => {
            // `cap_tool_output` only ever caps `Role::Tool` RESULT content,
            // never a `Role::Assistant` tool_call's `arguments` — so there is
            // no minted/recorded divergence to bridge here (the minted copy
            // IS the full copy), the same reasoning `ImageRedacted` uses.
            Ok(resolve_tool_input_value(
                &r.ptr,
                call_id,
                field,
                minted_view,
            )?)
        }
    }
}

/// Render a resolved `TurnsCleared` message range as readable text for
/// `expand_reduction`/`sidecar_search` — one role-labeled block per message,
/// carrying EVERYTHING the cleared messages held: text content, multimodal
/// `content_parts` (as their JSON), tool-result attribution
/// (`name`/`tool_call_id`), and every tool call's id, name, and full
/// arguments. Tool-call arguments matter most: content that exists ONLY
/// there (a `write_file` call's file body, a `bash` command line) would
/// otherwise be neither searchable nor expandable, and rescue-scenario
/// transcripts are full of exactly that.
fn render_turns(msgs: &[ChatMessage]) -> String {
    let mut out = String::new();
    for m in msgs {
        out.push_str(&format!("--- {:?}", m.role));
        if let Some(name) = &m.name {
            out.push_str(&format!(" name={name}"));
        }
        if let Some(id) = &m.tool_call_id {
            out.push_str(&format!(" tool_call_id={id}"));
        }
        out.push_str(" ---\n");
        if let Some(c) = &m.content {
            out.push_str(c);
            out.push('\n');
        }
        if let Some(parts) = &m.content_parts {
            for part in parts {
                out.push_str(&serde_json::to_string(part).unwrap_or_default());
                out.push('\n');
            }
        }
        for call in m.tool_calls() {
            out.push_str(&format!(
                "[tool call {} {}: {}]\n",
                call.id, call.function.name, call.function.arguments
            ));
        }
    }
    out
}

/// The byte span of `resolve_text(r, ..)`'s output that is CURRENTLY HIDDEN
/// from the live (reduced) view — what [`sidecar_search`] is allowed to
/// match against. [`ReductionKind::ToolOutputTruncated`]'s kept prefix
/// (`ptr.span`'s first element) is still visible in the view, so only the
/// remainder counts as hidden; every other kind's `ptr.span` is `None`
/// ("whole content removed", per [`super::SidecarPtr::span`]'s doc comment),
/// so the whole resolved text is hidden. `None` return means "not
/// searchable": [`ReductionKind::ImageRedacted`] (a `data:` URL's base64
/// payload is not a meaningful text-search target), and a
/// [`ReductionKind::DuplicateOutput`] whose byte-identical canonical
/// instance is still fully visible in the view — decided from `log` itself
/// ([`canonical_is_reduced`]), never by inspecting the live view. When the
/// canonical IS itself reduced (any kind, including sitting inside a
/// `TurnsCleared` range), the duplicate's whole content counts as hidden;
/// deliberately conservative for a `ToolOutputTruncated` canonical whose
/// kept prefix is still partially visible.
///
/// [`ReductionKind::OutputNormalized`] resolves to the RAW pre-normalization
/// bytes (ANSI codes and all, per [`resolve_text`]) — none of which are
/// literally present in the live view (which shows the normalized text
/// instead), so — like `FileReadElided`/`FileReadDiffed`/`TurnsCleared` — the
/// whole resolved text counts as hidden, not just a suffix.
///
/// [`ReductionKind::Superseded`] (TR-6) is, unconditionally, likewise fully
/// hidden — UNLIKE `DuplicateOutput`, its content is never required to be
/// byte-identical to its successor (`by`), so there is no "the same bytes
/// are already visible elsewhere" case to special-case away: whatever this
/// reduction hides is genuinely gone from the live view, full stop.
fn hidden_span(r: &Reduction, text: &str, log: &ReductionLog) -> Option<(usize, usize)> {
    match &r.kind {
        ReductionKind::ImageRedacted { .. } => None,
        ReductionKind::ToolOutputTruncated { .. } => {
            let kept = r.ptr.span.map(|(kept, _total)| kept).unwrap_or(0);
            let start = char_boundary_floor(text, kept.min(text.len()));
            Some((start, text.len()))
        }
        ReductionKind::DuplicateOutput { canonical, .. } => {
            if canonical_is_reduced(canonical.index, r, log) {
                Some((0, text.len()))
            } else {
                None // Canonical fully visible: these bytes are already in view.
            }
        }
        ReductionKind::FileReadElided { .. }
        | ReductionKind::FileReadDiffed { .. }
        | ReductionKind::OutputNormalized { .. }
        | ReductionKind::TurnsCleared { .. }
        | ReductionKind::ToolInputElided { .. }
        | ReductionKind::Superseded { .. } => Some((0, text.len())),
    }
}

/// Is the message at `canonical_index` reduced (its full content NOT
/// visible in the live view) according to `log`'s own records? True when any
/// record other than `this` either addresses that index directly (a
/// per-message reduction of any kind) or covers it with a `TurnsCleared`
/// range. Pure function of the log — the live view is never consulted.
fn canonical_is_reduced(canonical_index: usize, this: &Reduction, log: &ReductionLog) -> bool {
    log.reductions.iter().any(|other| {
        if other.id == this.id {
            return false;
        }
        match other.kind {
            ReductionKind::TurnsCleared { first, last, .. } => {
                canonical_index >= first && canonical_index <= last
            }
            _ => other.ptr.addr.index == canonical_index,
        }
    })
}

/// A char-boundary-safe window of [`SNIPPET_RADIUS`] bytes on each side of
/// `[start, end)` within `text`, hard-capped at [`SNIPPET_MAX_BYTES`] (a
/// huge regex match must not smuggle the whole hidden span back out through
/// its own snippet).
fn snippet_around(text: &str, start: usize, end: usize) -> String {
    let lo = char_boundary_floor(text, start.saturating_sub(SNIPPET_RADIUS));
    let hi_target = (end + SNIPPET_RADIUS).min(text.len());
    let mut hi = hi_target;
    while hi < text.len() && !text.is_char_boundary(hi) {
        hi += 1;
    }
    let window = &text[lo..hi.min(text.len())];
    let cap = char_boundary_floor(window, SNIPPET_MAX_BYTES);
    window[..cap].to_string()
}

// ---------------------------------------------------------------------------
// P4c (COMPOSABLE-HARNESS-DESIGN.md §5.2 "P4" core NEW-significant item,
// §1.10 "mid-session model switch", dep 8): reasoning-artifact filtering for
// cross-model handoff. §1.13 names THIS FILE as dep 8's home ("cross-model
// switches route through reasoning-artifact filtering (`reduce/rehydrate.rs`
// — catalog §5 dep 8)") — co-located with `expand_reduction`/`sidecar_search`
// because all three are the sidecar/cross-format-safety-relevant plumbing
// obligation 10 (model routing) and priority 2 (emulate-to-continue) share.
// ---------------------------------------------------------------------------

/// `ChatMessage::metadata` keys that carry a source model's private
/// reasoning/thinking payload — populated today by `Session`'s foreign-
/// format importers:
///
/// - Claude Code / Codex legacy singular fields (`session.rs`'s
///   `push_claude_assistant`/Codex `reasoning` `response_item` handling):
///   `"thinking"`/`"thinking_signature"`/`"redacted_thinking"`/
///   `"reasoning"`/`"reasoning_content"`/`"reasoning_encrypted"`.
/// - `"thinking_blocks"` — the Claude Code importer's exact per-block replay
///   list (`session.rs:3475-3480`, `push_claude_assistant`): every
///   `thinking`/`redacted_thinking` content block preserved SEPARATELY, in
///   order, each with its own signature/data, as a serialized JSON array.
///   REVIEW FINDING (P4c dep 8, MEDIUM, proven): the Claude Code EXPORTER
///   (`session.rs`'s `to_claude_code_jsonl`, ~5654-5676) PREFERS this key
///   over the legacy singular fields whenever present — so leaving it out of
///   this list let a full signed chain-of-thought survive `switch_model`
///   untouched and get re-emitted, re-attributed to model-B, on the very
///   next Claude Code export. Added here to close that hole.
/// - `"pi_thought_signature"` — pi's per-`toolCall`-block `thoughtSignature`
///   (Google-provider reasoning-continuity token, `session.rs:4248-4249`
///   import / `session.rs:7418-7419` export as `pi_assistant_content_value`
///   re-emits it onto every `toolCall` block, independent of whether any
///   `thinking`/`thinking_blocks` key is even present on the message).
///   Audited in alongside the fix above: like `redacted_thinking`, its
///   payload is opaque/encrypted rather than literal text, but this crate
///   already treats "opaque encrypted reasoning artifact" as reasoning-
///   bearing for `redacted_thinking` — `pi_thought_signature` is the same
///   class of thing (Google's opaque encoded reasoning trace tied to a tool
///   call), just pi-namespaced (`docs/interop/research/pi-fields.md:161`
///   groups it under "provider replay signatures" alongside
///   `thinking_signature`). Deliberately NOT added: `pi_text_signature`
///   (`session.rs:4219-4224`/`4271-4272`, OpenAI Responses replay-continuity
///   id for a `text` block whose content is already fully exposed via
///   `msg.content` — pi-fields.md's own per-field note calls it "replay-
///   continuity residue", not a reasoning payload) and `pi_thinking_redacted`
///   (`session.rs:4267-4269`, a bare boolean flag with no payload of its
///   own — and inert regardless, since `pi_assistant_content_value` only
///   ever reads it from inside the `if let Some(thinking) = ...` arm gated
///   on the now-stripped `"thinking"` key, so it can never reach an export
///   on its own). Stripping either would be over-stripping non-reasoning
///   metadata for no leak-closing benefit.
///
/// `metadata` itself is never serialized onto the wire (`ChatMessage`'s
/// custom `Serialize` impl omits it — see `message.rs`), so this list is a
/// DEFENSE-IN-DEPTH filter, not the primary leak-prevention mechanism: the
/// primary one is that `metadata` never reaches a provider request at all,
/// regardless of this function. What this filter actually guarantees is the
/// OBSERVABLE contract dep 8 asks for — a post-switch inspection of
/// `history` (the in-memory session state, sidecar exports, `/export`, a
/// future translator emitting this session under another harness's format)
/// never shows model-A's reasoning attributed to a conversation now being
/// driven by model-B.
pub const REASONING_METADATA_KEYS: &[&str] = &[
    "thinking",
    "thinking_signature",
    "redacted_thinking",
    "reasoning",
    "reasoning_content",
    "reasoning_encrypted",
    "thinking_blocks",
    "pi_thought_signature",
];

/// `content_parts` block `"type"` values that carry a reasoning payload as
/// model-VISIBLE content (as opposed to the metadata-only keys above) —
/// e.g. a future/foreign provider that echoes `{"type":"thinking",...}` or
/// `{"type":"reasoning",...}` blocks back into a message's content array
/// for continuation. None of supercode's own `content_parts` constructors
/// (`ChatMessage::user_with_images`/`tool_result_with_image`) ever produce
/// these types today, so this branch is defensive breadth against future/
/// foreign content rather than something exercised by supercode's own
/// native loop yet.
pub const REASONING_CONTENT_PART_TYPES: &[&str] = &["thinking", "reasoning", "redacted_thinking"];

/// Mid-session model switch (§1.10, dep 8): strip every reasoning artifact
/// out of `history` in place — both the metadata keys
/// ([`REASONING_METADATA_KEYS`]) and any `content_parts` blocks whose
/// `"type"` is in [`REASONING_CONTENT_PART_TYPES`] — so model-A's reasoning
/// never reaches model-B's context on the next request built from this
/// history. Returns the count of MESSAGES actually touched (had at least
/// one metadata key removed and/or at least one content part removed) —
/// `Agent::switch_model` records this in the persisted `model_change`
/// entry (`ModelChangeRecord::reasoning_artifacts_filtered`).
///
/// Pure and total: never panics, a message with nothing to filter is left
/// byte-identical (down to `content_parts` ordering of the parts that
/// survive), and calling this with nothing to filter (the common case —
/// supercode's own live loop never populates these keys) is a cheap no-op
/// scan, not a rebuild.
pub fn filter_reasoning_artifacts(history: &mut [ChatMessage]) -> usize {
    let mut touched = 0usize;
    for msg in history.iter_mut() {
        let mut this_touched = false;
        for key in REASONING_METADATA_KEYS {
            if msg.metadata.remove(*key).is_some() {
                this_touched = true;
            }
        }
        if let Some(parts) = msg.content_parts.as_mut() {
            let before = parts.len();
            parts.retain(|p| {
                p.get("type")
                    .and_then(|t| t.as_str())
                    .map(|t| !REASONING_CONTENT_PART_TYPES.contains(&t))
                    .unwrap_or(true)
            });
            if parts.len() != before {
                this_touched = true;
            }
        }
        if this_touched {
            touched += 1;
        }
    }
    touched
}