shore-protocol 0.7.0

Shore Wire Protocol (SWP) message types and serialization for the Silvershore chat daemon.
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
//! Tool-loop message merging for client consumption.
//!
//! Storage keeps separate messages (assistant tool_use, user tool_result,
//! assistant text) for LLM API compatibility. This module collapses them
//! into single assistant messages for client display:
//!
//! ```text
//! [user, asst(tool_use), user(tool_result), asst(text)]
//!   -> [user, asst(thinking + tool_use + tool_result + text)]
//! ```

use crate::types::{ContentBlock, Message, Role};

/// A "tool loop assistant" has ToolUse blocks.
///
/// The model may emit text before calling tools ("let me check...") — this
/// text is still part of the tool loop and gets merged into the final
/// assistant message's content_blocks.
fn is_tool_loop_assistant(msg: &Message) -> bool {
    msg.role == Role::Assistant
        && msg
            .content_blocks
            .iter()
            .any(|b| matches!(b, ContentBlock::ToolUse { .. }))
}

/// A "tool result user" message has ONLY ToolResult blocks.
fn is_tool_result_only(msg: &Message) -> bool {
    msg.role == Role::User
        && !msg.content_blocks.is_empty()
        && msg
            .content_blocks
            .iter()
            .all(|b| matches!(b, ContentBlock::ToolResult { .. }))
}

/// Derive content from Text blocks only (for merged messages).
///
/// Excludes ToolResult content because merged messages embed tool results
/// in content_blocks.
fn derive_content_text_only(blocks: &[ContentBlock]) -> String {
    super::types::derive_content_from_blocks_with(blocks, false)
}

/// Collect blocks from one tool-loop round (assistant + user result pair).
///
/// Preserves block ordering: text and thinking blocks are emitted in their
/// original position, while each ToolUse is followed by its matching
/// ToolResult (matched by `id` == `tool_use_id`).
fn collect_round(assistant: &Message, results: Option<&Message>, out: &mut Vec<ContentBlock>) {
    for block in &assistant.content_blocks {
        match block {
            ContentBlock::ToolUse { id, .. } => {
                out.push(block.clone());
                // Find and emit the matching tool result.
                if let Some(result_msg) = results {
                    if let Some(tr) = result_msg.content_blocks.iter().find(|b| {
                        matches!(b, ContentBlock::ToolResult { tool_use_id, .. } if tool_use_id == id)
                    }) {
                        out.push(tr.clone());
                    }
                }
            }
            // Text, Thinking, RedactedThinking — emit in place.
            ContentBlock::Text { text } if text.trim().is_empty() => {
                // Skip whitespace-only text blocks (LLM noise).
            }
            ContentBlock::Text { .. }
            | ContentBlock::Thinking { .. }
            | ContentBlock::RedactedThinking { .. }
            | ContentBlock::ToolResult { .. } => {
                out.push(block.clone());
            }
        }
    }
}

/// Merge tool-loop messages into logical assistant turns.
///
/// The merged message's `content_blocks` contain all blocks in interleaved
/// order: thinking blocks, then (tool_use, tool_result) pairs per round,
/// then text. The `content` field is derived from Text blocks only.
///
/// Messages without tool loops pass through unchanged. Tool-result-only
/// user messages are consumed by the merge and do not appear in output.
pub fn merge_tool_loop_messages(messages: &[Message]) -> Vec<Message> {
    let mut output: Vec<Message> = Vec::new();
    let mut i = 0;

    while let Some(msg) = messages.get(i) {
        // Non-assistant messages: pass through unless tool-result-only.
        if msg.role != Role::Assistant {
            if !is_tool_result_only(msg) {
                output.push(msg.clone());
            }
            i = i.saturating_add(1);
            continue;
        }

        // Assistant with Text blocks (not a tool-loop intermediate): pass through.
        if !is_tool_loop_assistant(msg) {
            output.push(msg.clone());
            i = i.saturating_add(1);
            continue;
        }

        // ── Tool loop detected ──────────────────────────────────────────
        let mut merged_blocks: Vec<ContentBlock> = Vec::new();
        let mut last_assistant = msg;

        while let Some(current) = messages.get(i) {
            // Peek at next for tool results.
            let results = i
                .checked_add(1)
                .and_then(|next_index| messages.get(next_index))
                .filter(|next| is_tool_result_only(next));
            let next_is_result = results.is_some();

            collect_round(current, results, &mut merged_blocks);
            last_assistant = current;

            // Consume the pair (or just the assistant if no result yet).
            if next_is_result {
                i = i.saturating_add(2);
            } else {
                i = i.saturating_add(1);
            }

            // Check what comes next.
            if i >= messages.len() {
                break; // End of conversation (incomplete loop).
            }

            let Some(next) = messages.get(i) else {
                break;
            };
            if next.role == Role::Assistant && is_tool_loop_assistant(next) {
                continue; // Another tool-loop round.
            }

            if next.role == Role::Assistant {
                // Final assistant message with text — append its blocks and finish.
                merged_blocks.extend(next.content_blocks.iter().cloned());
                last_assistant = next;
                i = i.saturating_add(1);
                break;
            }

            // Non-assistant (unexpected mid-loop, or just next user message).
            break;
        }

        // Build the merged message.
        let content = derive_content_text_only(&merged_blocks);
        output.push(Message {
            msg_id: last_assistant.msg_id.clone(),
            role: Role::Assistant,
            content,
            images: last_assistant.images.clone(),
            content_blocks: merged_blocks,
            alt_index: last_assistant.alt_index,
            alt_count: last_assistant.alt_count,
            alternatives: last_assistant.alternatives.clone(),
            timestamp: last_assistant.timestamp.clone(),
            provider_key: last_assistant.provider_key.clone(),
        });
    }

    output
}

// ── Tests ────────────────────────────────────────────────────────────────

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

    fn make_msg(id: &str, role: Role, content: &str, blocks: Vec<ContentBlock>) -> Message {
        Message {
            msg_id: id.into(),
            role,
            content: content.into(),
            images: vec![],
            content_blocks: blocks,
            alt_index: None,
            alt_count: None,
            alternatives: vec![],
            provider_key: None,
            timestamp: "2026-01-01T00:00:00Z".into(),
        }
    }

    fn user_msg(id: &str, content: &str) -> Message {
        make_msg(id, Role::User, content, vec![])
    }

    fn assistant_text(id: &str, text: &str) -> Message {
        make_msg(
            id,
            Role::Assistant,
            text,
            vec![ContentBlock::Text { text: text.into() }],
        )
    }

    fn assistant_tool_use(id: &str, tools: Vec<(&str, &str)>) -> Message {
        let blocks: Vec<ContentBlock> = tools
            .into_iter()
            .map(|(tid, name)| ContentBlock::ToolUse {
                id: tid.into(),
                name: name.into(),
                input: json!({}),
            })
            .collect();
        make_msg(id, Role::Assistant, "", blocks)
    }

    fn assistant_thinking_and_tool_use(
        id: &str,
        thinking: &str,
        tools: Vec<(&str, &str)>,
    ) -> Message {
        let mut blocks = vec![ContentBlock::Thinking {
            thinking: thinking.into(),
            signature: None,
        }];
        for (tid, name) in tools {
            blocks.push(ContentBlock::ToolUse {
                id: tid.into(),
                name: name.into(),
                input: json!({}),
            });
        }
        make_msg(id, Role::Assistant, "", blocks)
    }

    fn user_tool_results(id: &str, results: Vec<(&str, &str, bool)>) -> Message {
        let blocks: Vec<ContentBlock> = results
            .into_iter()
            .map(|(tid, content, is_error)| ContentBlock::ToolResult {
                tool_use_id: tid.into(),
                content: content.into(),
                is_error,
            })
            .collect();
        let content = crate::types::derive_content_from_blocks(&blocks);
        make_msg(id, Role::User, &content, blocks)
    }

    fn item<T>(items: &[T], index: usize) -> &T {
        items.get(index).expect("expected item")
    }

    // ── Basic cases ─────────────────────────────────────────────────

    #[test]
    fn empty_conversation() {
        assert!(merge_tool_loop_messages(&[]).is_empty());
    }

    #[test]
    fn no_tool_use() {
        let msgs = vec![user_msg("u1", "hello"), assistant_text("a1", "hi there")];
        let merged = merge_tool_loop_messages(&msgs);
        assert_eq!(merged.len(), 2);
        assert_eq!(item(&merged, 0).msg_id, "u1");
        assert_eq!(item(&merged, 1).msg_id, "a1");
    }

    #[test]
    fn whitespace_text_block_does_not_prevent_merge() {
        // Real-world: LLM emits "\n\n" text block before thinking/tool_use.
        let mut asst = assistant_thinking_and_tool_use(
            "a1",
            "Let me check",
            vec![("t1", "memory_search"), ("t2", "check_time")],
        );
        // Insert a whitespace-only Text block at the front (as the LLM does).
        asst.content_blocks.insert(
            0,
            ContentBlock::Text {
                text: "\n\n".into(),
            },
        );

        let msgs = vec![
            user_msg("u1", "hello"),
            asst,
            user_tool_results(
                "u2",
                vec![("t1", "mem result", false), ("t2", "3:22 PM", false)],
            ),
            assistant_text("a2", "Hey there!"),
        ];
        let merged = merge_tool_loop_messages(&msgs);
        assert_eq!(merged.len(), 2, "should merge into user + assistant");
        let assistant = item(&merged, 1);
        assert_eq!(assistant.msg_id, "a2");
        assert_eq!(assistant.content, "Hey there!");

        // Blocks: thinking, tu(memory_search), tr(memory_search), tu(check_time), tr(check_time), text
        let blocks = &assistant.content_blocks;
        assert_eq!(blocks.len(), 6);
        assert!(matches!(item(blocks, 0), ContentBlock::Thinking { .. }));
        assert!(
            matches!(item(blocks, 1), ContentBlock::ToolUse { name, .. } if name == "memory_search")
        );
        assert!(matches!(item(blocks, 2), ContentBlock::ToolResult { .. }));
        assert!(
            matches!(item(blocks, 3), ContentBlock::ToolUse { name, .. } if name == "check_time")
        );
        assert!(matches!(item(blocks, 4), ContentBlock::ToolResult { .. }));
        assert!(matches!(item(blocks, 5), ContentBlock::Text { text } if text == "Hey there!"));
    }

    #[test]
    fn text_before_tool_calls_merged() {
        // Real-world: model says "let me check" then calls tools.
        let mut asst =
            assistant_tool_use("a1", vec![("t1", "memory_search"), ("t2", "check_time")]);
        asst.content_blocks.insert(
            0,
            ContentBlock::Text {
                text: "let me look that up!".into(),
            },
        );

        let msgs = vec![
            user_msg("u1", "what do you know about me?"),
            asst,
            user_tool_results(
                "u2",
                vec![("t1", "Trevor", false), ("t2", "3:22 PM", false)],
            ),
            assistant_text("a2", "You're Trevor!"),
        ];
        let merged = merge_tool_loop_messages(&msgs);
        assert_eq!(merged.len(), 2, "should merge into user + assistant");
        let assistant = item(&merged, 1);
        assert_eq!(assistant.msg_id, "a2");

        // Blocks: text("let me look..."), tu(memory_search), tr(memory_search), tu(check_time), tr(check_time), text("You're Trevor!")
        let blocks = &assistant.content_blocks;
        assert_eq!(blocks.len(), 6);
        assert!(
            matches!(item(blocks, 0), ContentBlock::Text { text } if text == "let me look that up!")
        );
        assert!(
            matches!(item(blocks, 1), ContentBlock::ToolUse { name, .. } if name == "memory_search")
        );
        assert!(matches!(item(blocks, 2), ContentBlock::ToolResult { .. }));
        assert!(
            matches!(item(blocks, 3), ContentBlock::ToolUse { name, .. } if name == "check_time")
        );
        assert!(matches!(item(blocks, 4), ContentBlock::ToolResult { .. }));
        assert!(matches!(item(blocks, 5), ContentBlock::Text { text } if text == "You're Trevor!"));

        // content includes both text blocks
        assert!(assistant.content.contains("let me look that up!"));
        assert!(assistant.content.contains("You're Trevor!"));
    }

    #[test]
    fn single_tool_round() {
        let msgs = vec![
            user_msg("u1", "what time is it?"),
            assistant_tool_use("a1", vec![("t1", "check_time")]),
            user_tool_results("u2", vec![("t1", "3:22 PM", false)]),
            assistant_text("a2", "It's 3:22 PM!"),
        ];
        let merged = merge_tool_loop_messages(&msgs);
        assert_eq!(merged.len(), 2);
        assert_eq!(item(&merged, 0).msg_id, "u1");
        let assistant = item(&merged, 1);
        assert_eq!(assistant.msg_id, "a2");
        assert_eq!(assistant.content, "It's 3:22 PM!");

        // Check blocks: tool_use, tool_result, text
        let blocks = &assistant.content_blocks;
        assert_eq!(blocks.len(), 3);
        assert!(
            matches!(item(blocks, 0), ContentBlock::ToolUse { name, .. } if name == "check_time")
        );
        assert!(
            matches!(item(blocks, 1), ContentBlock::ToolResult { content, .. } if content == "3:22 PM")
        );
        assert!(matches!(item(blocks, 2), ContentBlock::Text { text } if text == "It's 3:22 PM!"));
    }

    #[test]
    fn multiple_tools_single_round() {
        let msgs = vec![
            user_msg("u1", "time and save"),
            assistant_tool_use("a1", vec![("t1", "check_time"), ("t2", "memory_write")]),
            user_tool_results("u2", vec![("t1", "3:22 PM", false), ("t2", "saved", false)]),
            assistant_text("a2", "Done!"),
        ];
        let merged = merge_tool_loop_messages(&msgs);
        assert_eq!(merged.len(), 2);

        let blocks = &item(&merged, 1).content_blocks;
        assert_eq!(blocks.len(), 5); // tu1, tr1, tu2, tr2, text
        assert!(matches!(item(blocks, 0), ContentBlock::ToolUse { id, .. } if id == "t1"));
        assert!(
            matches!(item(blocks, 1), ContentBlock::ToolResult { tool_use_id, .. } if tool_use_id == "t1")
        );
        assert!(matches!(item(blocks, 2), ContentBlock::ToolUse { id, .. } if id == "t2"));
        assert!(
            matches!(item(blocks, 3), ContentBlock::ToolResult { tool_use_id, .. } if tool_use_id == "t2")
        );
        assert!(matches!(item(blocks, 4), ContentBlock::Text { .. }));
    }

    #[test]
    fn multi_round_tool_loop() {
        let msgs = vec![
            user_msg("u1", "do stuff"),
            assistant_tool_use("a1", vec![("t1", "search")]),
            user_tool_results("u2", vec![("t1", "result A", false)]),
            assistant_tool_use("a2", vec![("t2", "fetch")]),
            user_tool_results("u3", vec![("t2", "result B", false)]),
            assistant_text("a3", "Here you go."),
        ];
        let merged = merge_tool_loop_messages(&msgs);
        assert_eq!(merged.len(), 2);
        assert_eq!(item(&merged, 1).msg_id, "a3");

        let blocks = &item(&merged, 1).content_blocks;
        assert_eq!(blocks.len(), 5); // tu1, tr1, tu2, tr2, text
    }

    // ── Thinking ────────────────────────────────────────────────────

    #[test]
    fn thinking_preserved() {
        let msgs = vec![
            user_msg("u1", "remember me?"),
            assistant_thinking_and_tool_use(
                "a1",
                "Let me search memory",
                vec![("t1", "memory_search")],
            ),
            user_tool_results("u2", vec![("t1", "Trevor", false)]),
            assistant_text("a2", "Yes, you're Trevor!"),
        ];
        let merged = merge_tool_loop_messages(&msgs);
        assert_eq!(merged.len(), 2);

        let blocks = &item(&merged, 1).content_blocks;
        assert_eq!(blocks.len(), 4); // thinking, tool_use, tool_result, text
        assert!(
            matches!(item(blocks, 0), ContentBlock::Thinking { thinking, .. } if thinking == "Let me search memory")
        );
        assert!(matches!(item(blocks, 1), ContentBlock::ToolUse { .. }));
        assert!(matches!(item(blocks, 2), ContentBlock::ToolResult { .. }));
        assert!(matches!(item(blocks, 3), ContentBlock::Text { .. }));
    }

    #[test]
    fn redacted_thinking_preserved() {
        let mut msgs = vec![user_msg("u1", "test")];
        // Assistant with redacted thinking + tool use.
        let blocks = vec![
            ContentBlock::RedactedThinking {
                data: "opaque".into(),
            },
            ContentBlock::ToolUse {
                id: "t1".into(),
                name: "search".into(),
                input: json!({}),
            },
        ];
        msgs.push(make_msg("a1", Role::Assistant, "", blocks.clone()));
        msgs.push(user_tool_results("u2", vec![("t1", "found", false)]));
        msgs.push(assistant_text("a2", "Result"));

        let merged = merge_tool_loop_messages(&msgs);
        assert_eq!(merged.len(), 2);
        assert!(matches!(
            item(&item(&merged, 1).content_blocks, 0),
            ContentBlock::RedactedThinking { .. }
        ));
    }

    // ── Incomplete loops ────────────────────────────────────────────

    #[test]
    fn incomplete_loop_no_result() {
        let msgs = vec![
            user_msg("u1", "test"),
            assistant_tool_use("a1", vec![("t1", "search")]),
        ];
        let merged = merge_tool_loop_messages(&msgs);
        assert_eq!(merged.len(), 2);
        let assistant = item(&merged, 1);
        assert_eq!(assistant.msg_id, "a1");
        assert_eq!(assistant.content_blocks.len(), 1);
        assert!(matches!(
            item(&assistant.content_blocks, 0),
            ContentBlock::ToolUse { .. }
        ));
    }

    #[test]
    fn incomplete_loop_mid_chain() {
        let msgs = vec![
            user_msg("u1", "test"),
            assistant_tool_use("a1", vec![("t1", "search")]),
            user_tool_results("u2", vec![("t1", "result", false)]),
            assistant_tool_use("a2", vec![("t2", "fetch")]),
        ];
        let merged = merge_tool_loop_messages(&msgs);
        assert_eq!(merged.len(), 2);
        assert_eq!(item(&merged, 1).msg_id, "a2");

        let blocks = &item(&merged, 1).content_blocks;
        assert_eq!(blocks.len(), 3); // tu1, tr1, tu2 (no tr2, no text)
    }

    // ── Content field ───────────────────────────────────────────────

    #[test]
    fn content_field_text_only() {
        let msgs = vec![
            user_msg("u1", "test"),
            assistant_tool_use("a1", vec![("t1", "check_time")]),
            user_tool_results("u2", vec![("t1", "3:22 PM", false)]),
            assistant_text("a2", "The time is 3:22 PM."),
        ];
        let merged = merge_tool_loop_messages(&msgs);
        // Content should be the text response only, not tool results.
        assert_eq!(item(&merged, 1).content, "The time is 3:22 PM.");
    }

    #[test]
    fn content_field_empty_for_incomplete() {
        let msgs = vec![
            user_msg("u1", "test"),
            assistant_tool_use("a1", vec![("t1", "search")]),
        ];
        let merged = merge_tool_loop_messages(&msgs);
        assert_eq!(item(&merged, 1).content, "");
    }

    // ── Metadata inheritance ────────────────────────────────────────

    #[test]
    fn merged_msg_inherits_final_metadata() {
        let mut final_msg = assistant_text("a_final", "Done");
        final_msg.timestamp = "2026-03-29T15:30:00Z".into();
        final_msg.alt_index = Some(1);
        final_msg.alt_count = Some(3);

        let msgs = vec![
            user_msg("u1", "test"),
            assistant_tool_use("a1", vec![("t1", "search")]),
            user_tool_results("u2", vec![("t1", "found", false)]),
            final_msg,
        ];
        let merged = merge_tool_loop_messages(&msgs);
        let assistant = item(&merged, 1);
        assert_eq!(assistant.msg_id, "a_final");
        assert_eq!(assistant.timestamp, "2026-03-29T15:30:00Z");
        assert_eq!(assistant.alt_index, Some(1));
        assert_eq!(assistant.alt_count, Some(3));
    }

    // ── Filtering ───────────────────────────────────────────────────

    #[test]
    fn orphan_tool_result_user_filtered() {
        let msgs = vec![
            user_msg("u1", "hello"),
            user_tool_results("u2", vec![("t1", "orphan result", false)]),
            assistant_text("a1", "hi"),
        ];
        let merged = merge_tool_loop_messages(&msgs);
        assert_eq!(merged.len(), 2);
        assert_eq!(item(&merged, 0).msg_id, "u1");
        assert_eq!(item(&merged, 1).msg_id, "a1");
    }

    #[test]
    fn normal_user_messages_preserved() {
        let msgs = vec![
            user_msg("u1", "first"),
            assistant_text("a1", "reply 1"),
            user_msg("u2", "second"),
            assistant_text("a2", "reply 2"),
        ];
        let merged = merge_tool_loop_messages(&msgs);
        assert_eq!(merged.len(), 4);
    }

    #[test]
    fn system_messages_preserved() {
        let msgs = vec![
            make_msg("s1", Role::System, "system prompt", vec![]),
            user_msg("u1", "hello"),
            assistant_text("a1", "hi"),
        ];
        let merged = merge_tool_loop_messages(&msgs);
        assert_eq!(merged.len(), 3);
        assert_eq!(item(&merged, 0).role, Role::System);
    }

    // ── Multiple exchanges ──────────────────────────────────────────

    #[test]
    fn multiple_exchanges_only_first_has_tools() {
        let msgs = vec![
            user_msg("u1", "time?"),
            assistant_tool_use("a1", vec![("t1", "check_time")]),
            user_tool_results("u2", vec![("t1", "3:22 PM", false)]),
            assistant_text("a2", "It's 3:22 PM"),
            user_msg("u3", "thanks"),
            assistant_text("a3", "You're welcome!"),
        ];
        let merged = merge_tool_loop_messages(&msgs);
        assert_eq!(merged.len(), 4);
        assert_eq!(item(&merged, 0).msg_id, "u1");
        assert_eq!(item(&merged, 1).msg_id, "a2"); // merged
        assert_eq!(item(&merged, 2).msg_id, "u3");
        assert_eq!(item(&merged, 3).msg_id, "a3"); // passthrough
    }

    #[test]
    fn both_exchanges_have_tools() {
        let msgs = vec![
            user_msg("u1", "time?"),
            assistant_tool_use("a1", vec![("t1", "check_time")]),
            user_tool_results("u2", vec![("t1", "3:22 PM", false)]),
            assistant_text("a2", "3:22"),
            user_msg("u3", "remember me"),
            assistant_tool_use("a3", vec![("t2", "memory_search")]),
            user_tool_results("u4", vec![("t2", "Trevor", false)]),
            assistant_text("a4", "Hi Trevor!"),
        ];
        let merged = merge_tool_loop_messages(&msgs);
        assert_eq!(merged.len(), 4);
        assert_eq!(item(&merged, 1).content_blocks.len(), 3); // tu, tr, text
        assert_eq!(item(&merged, 3).content_blocks.len(), 3); // tu, tr, text
    }

    // ── Legacy messages ─────────────────────────────────────────────

    #[test]
    fn legacy_messages_without_content_blocks() {
        let msgs = vec![
            make_msg("u1", Role::User, "old message", vec![]),
            make_msg("a1", Role::Assistant, "old reply", vec![]),
        ];
        let merged = merge_tool_loop_messages(&msgs);
        assert_eq!(merged.len(), 2);
        assert_eq!(item(&merged, 0).content, "old message");
        assert_eq!(item(&merged, 1).content, "old reply");
    }

    // ── Error results ───────────────────────────────────────────────

    #[test]
    fn unmatched_tool_use_id_no_result_paired() {
        // Assistant calls tool "t1" but result message has "t999" — no match.
        let msgs = vec![
            user_msg("u1", "test"),
            assistant_tool_use("a1", vec![("t1", "search")]),
            user_tool_results("u2", vec![("t999", "orphan result", false)]),
            assistant_text("a2", "Done"),
        ];
        let merged = merge_tool_loop_messages(&msgs);
        assert_eq!(merged.len(), 2);
        // ToolUse present but no ToolResult paired (mismatched IDs).
        let blocks = &item(&merged, 1).content_blocks;
        assert!(matches!(item(blocks, 0), ContentBlock::ToolUse { id, .. } if id == "t1"));
        // No ToolResult for t1 in the merged output.
        assert!(
            !blocks.iter().any(
                |b| matches!(b, ContentBlock::ToolResult { tool_use_id, .. } if tool_use_id == "t1")
            ),
            "should not have a tool result for t1"
        );
    }

    #[test]
    fn multiple_tool_uses_partial_results() {
        // Two tools called, but only one has a matching result.
        let msgs = vec![
            user_msg("u1", "test"),
            assistant_tool_use("a1", vec![("t1", "search"), ("t2", "fetch")]),
            user_tool_results("u2", vec![("t1", "found it", false)]),
            assistant_text("a2", "Result"),
        ];
        let merged = merge_tool_loop_messages(&msgs);
        let blocks = &item(&merged, 1).content_blocks;
        // t1 should have its result paired, t2 should not.
        assert!(matches!(item(blocks, 0), ContentBlock::ToolUse { id, .. } if id == "t1"));
        assert!(
            matches!(item(blocks, 1), ContentBlock::ToolResult { tool_use_id, .. } if tool_use_id == "t1")
        );
        assert!(matches!(item(blocks, 2), ContentBlock::ToolUse { id, .. } if id == "t2"));
        // No ToolResult for t2.
        assert!(!blocks.iter().any(
            |b| matches!(b, ContentBlock::ToolResult { tool_use_id, .. } if tool_use_id == "t2")
        ));
    }

    #[test]
    fn tool_error_results_preserved() {
        let msgs = vec![
            user_msg("u1", "search something"),
            assistant_tool_use("a1", vec![("t1", "web_search")]),
            user_tool_results("u2", vec![("t1", "Connection refused", true)]),
            assistant_text("a2", "Sorry, the search failed."),
        ];
        let merged = merge_tool_loop_messages(&msgs);
        let blocks = &item(&merged, 1).content_blocks;
        assert!(matches!(
            item(blocks, 1),
            ContentBlock::ToolResult { is_error, content, .. }
            if *is_error && content == "Connection refused"
        ));
    }
}