dirge-agent 0.7.4

Minimalistic coding agent written in Rust, optimized for memory footprint and performance
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
use super::*;
use crate::agent::agent_loop::message::{
    AssistantMessage, ContentBlock, StopReason, ToolResultMessage, UserMessage,
};
use crate::agent::agent_loop::result::LoopToolResult;

/// Convenience: build a partial assistant message with a single
/// text block.
fn assistant_with_text(s: &str) -> AssistantMessage {
    AssistantMessage::new(
        vec![ContentBlock::Text {
            text: s.to_string(),
        }],
        StopReason::Stop,
    )
}

/// Convenience: assistant with thinking content.
fn assistant_with_thinking(s: &str) -> AssistantMessage {
    AssistantMessage::new(
        vec![ContentBlock::Thinking {
            text: s.to_string(),
        }],
        StopReason::Stop,
    )
}

/// `TurnStart` increments the counter; the emitted event
/// carries the value PRIOR to the increment (so the first
/// TurnStart is index 0). `TurnEnd` matches.
#[test]
fn turn_start_end_index_round_trips() {
    let mut bridge = EventBridge::new();
    let s0 = bridge.translate(LoopEvent::TurnStart);
    let e0 = bridge.translate(LoopEvent::TurnEnd {
        message: assistant_with_text("hi"),
        tool_results: Vec::new(),
    });
    let s1 = bridge.translate(LoopEvent::TurnStart);
    let e1 = bridge.translate(LoopEvent::TurnEnd {
        message: assistant_with_text("again"),
        tool_results: Vec::new(),
    });

    assert!(matches!(
        s0.as_slice(),
        [AgentEvent::TurnStart { index: 0 }]
    ));
    assert!(matches!(e0.as_slice(), [AgentEvent::TurnEnd { index: 0 }]));
    assert!(matches!(
        s1.as_slice(),
        [AgentEvent::TurnStart { index: 1 }]
    ));
    assert!(matches!(e1.as_slice(), [AgentEvent::TurnEnd { index: 1 }]));
}

/// `AgentStart` is a no-op — dirge has no equivalent event.
/// `AgentEnd` produces `Done` with the final assistant text.
#[test]
fn agent_start_no_op_agent_end_emits_done() {
    let mut bridge = EventBridge::new();
    assert!(bridge.translate(LoopEvent::AgentStart).is_empty());

    let messages = vec![
        LoopMessage::User(UserMessage {
            content: "hi".to_string(),
        }),
        LoopMessage::Assistant(assistant_with_text("final answer")),
    ];
    let out = bridge.translate(LoopEvent::AgentEnd { messages });
    assert_eq!(out.len(), 1);
    match &out[0] {
        AgentEvent::Done {
            response,
            tokens,
            cost,
        } => {
            assert_eq!(response.as_str(), "final answer");
            assert_eq!(*tokens, 0);
            assert_eq!(*cost, 0.0);
        }
        _ => panic!("expected Done"),
    }
}

/// Phase 4.5h-1: AgentEnd carrying an assistant message with
/// stop_reason=Error + a context-length error string →
/// `AgentEvent::ContextOverflow` (not `Done` or `Error`).
/// UI consumes ContextOverflow by running `/compress` and
/// respawning a fresh runner with the same prompt against
/// the compacted history.
#[test]
fn agent_end_context_length_error_emits_context_overflow() {
    let mut bridge = EventBridge::new();
    let mut a = assistant_with_text("");
    a.stop_reason = StopReason::Error;
    a.error_message = Some("prompt is too long: maximum context length exceeded".to_string());
    let messages = vec![
        LoopMessage::User(UserMessage {
            content: "summarize this huge doc".to_string(),
        }),
        LoopMessage::Assistant(a),
    ];
    let out = bridge.translate(LoopEvent::AgentEnd { messages });
    assert_eq!(out.len(), 1);
    match &out[0] {
        AgentEvent::ContextOverflow { prompt, error } => {
            assert_eq!(prompt.as_str(), "summarize this huge doc");
            assert!(
                error.contains("context length") || error.contains("too long"),
                "error text should mention context length"
            );
        }
        other => panic!("expected ContextOverflow, got {other:?}"),
    }
}

/// Phase 4.5h-1: AgentEnd with stop_reason=Error but NOT a
/// context-length signal → `AgentEvent::Error`.
#[test]
fn agent_end_non_context_error_emits_error() {
    let mut bridge = EventBridge::new();
    let mut a = assistant_with_text("");
    a.stop_reason = StopReason::Error;
    a.error_message = Some("401 unauthorized: invalid api key".to_string());
    let messages = vec![
        LoopMessage::User(UserMessage {
            content: "hi".to_string(),
        }),
        LoopMessage::Assistant(a),
    ];
    let out = bridge.translate(LoopEvent::AgentEnd { messages });
    assert_eq!(out.len(), 1);
    match &out[0] {
        AgentEvent::Error(msg) => {
            assert!(msg.contains("unauthorized"));
        }
        other => panic!("expected Error, got {other:?}"),
    }
}

/// Interject channel cancellation surfaces as a stream Error
/// with the message "stream aborted by cancellation signal"
/// (rig_stream.rs:124). The bridge must recognise this as a
/// graceful interjection — emit `Interjected` so the UI drains
/// its queued messages, NOT `Error` which would drop them
/// (the user's bug report on this).
#[test]
fn agent_end_cancellation_emits_interjected_not_error() {
    let mut bridge = EventBridge::new();
    let mut a = assistant_with_text("partial response before interject");
    a.stop_reason = StopReason::Error;
    a.error_message = Some("stream aborted by cancellation signal".to_string());
    let messages = vec![
        LoopMessage::User(UserMessage {
            content: "do a long thing".to_string(),
        }),
        LoopMessage::Assistant(a),
    ];
    let out = bridge.translate(LoopEvent::AgentEnd { messages });
    assert_eq!(out.len(), 1);
    match &out[0] {
        AgentEvent::Interjected {
            partial_response,
            tokens,
        } => {
            assert_eq!(
                partial_response.as_str(),
                "partial response before interject"
            );
            assert_eq!(*tokens, 0);
        }
        other => panic!("expected Interjected, got {other:?}"),
    }
}

/// Phase 4.5h-1: AgentEnd with stop_reason=Error but no
/// error_message → still emits Error with a placeholder
/// message. Defensive — error variant should never produce
/// a misleading Done.
#[test]
fn agent_end_error_without_message_still_emits_error() {
    let mut bridge = EventBridge::new();
    let mut a = assistant_with_text("");
    a.stop_reason = StopReason::Error;
    a.error_message = None;
    let messages = vec![LoopMessage::Assistant(a)];
    let out = bridge.translate(LoopEvent::AgentEnd { messages });
    assert_eq!(out.len(), 1);
    assert!(matches!(out[0], AgentEvent::Error(_)));
}

/// AgentEnd with stop_reason=Aborted → Done with empty
/// response. Graceful abort doesn't produce an error event.
/// Interjected (the UI's "user said stop") is a runner-level
/// concern handled separately.
#[test]
fn agent_end_aborted_emits_done() {
    let mut bridge = EventBridge::new();
    let mut a = assistant_with_text("partial work");
    a.stop_reason = StopReason::Aborted;
    let messages = vec![LoopMessage::Assistant(a)];
    let out = bridge.translate(LoopEvent::AgentEnd { messages });
    assert_eq!(out.len(), 1);
    // Done — the loop ended (no Error). Response field carries
    // whatever text had assembled.
    match &out[0] {
        AgentEvent::Done { response, .. } => {
            assert_eq!(response.as_str(), "partial work");
        }
        other => panic!("expected Done, got {other:?}"),
    }
}

/// `AgentEnd` with no assistant message → `Done` with empty
/// response.
#[test]
fn agent_end_no_assistant_done_empty_response() {
    let mut bridge = EventBridge::new();
    let messages = vec![LoopMessage::User(UserMessage {
        content: "hi".to_string(),
    })];
    let out = bridge.translate(LoopEvent::AgentEnd { messages });
    match &out[0] {
        AgentEvent::Done { response, .. } => {
            assert_eq!(response.as_str(), "");
        }
        _ => panic!("expected Done"),
    }
}

/// `MessageUpdate { TextStart }` emits a single `Token` with
/// the initial text chunk. The bridge tracks this as
/// "last_text_emitted" so subsequent deltas emit only the new
/// portion.
#[test]
fn text_delta_emits_token_chunks() {
    let mut bridge = EventBridge::new();
    // First chunk: "Hello"
    let out = bridge.translate(LoopEvent::MessageUpdate {
        message: assistant_with_text("Hello"),
        phase: DeltaPhase::TextStart,
    });
    assert_eq!(out.len(), 1);
    match &out[0] {
        AgentEvent::Token(s) => assert_eq!(s.as_str(), "Hello"),
        _ => panic!("expected Token"),
    }
    // Second chunk: "Hello world" (provider appended " world")
    let out = bridge.translate(LoopEvent::MessageUpdate {
        message: assistant_with_text("Hello world"),
        phase: DeltaPhase::TextDelta,
    });
    assert_eq!(out.len(), 1);
    match &out[0] {
        AgentEvent::Token(s) => assert_eq!(s.as_str(), " world"),
        _ => panic!("expected Token"),
    }
    // Third update with no new text → no event.
    let out = bridge.translate(LoopEvent::MessageUpdate {
        message: assistant_with_text("Hello world"),
        phase: DeltaPhase::TextDelta,
    });
    assert!(out.is_empty());
}

/// `MessageUpdate` for reasoning produces `Reasoning` events
/// using the same delta tracking.
#[test]
fn reasoning_delta_emits_reasoning_chunks() {
    let mut bridge = EventBridge::new();
    let out = bridge.translate(LoopEvent::MessageUpdate {
        message: assistant_with_thinking("Let me think"),
        phase: DeltaPhase::ThinkingStart,
    });
    match &out[0] {
        AgentEvent::Reasoning(s) => assert_eq!(s.as_str(), "Let me think"),
        _ => panic!("expected Reasoning"),
    }
    let out = bridge.translate(LoopEvent::MessageUpdate {
        message: assistant_with_thinking("Let me think about this"),
        phase: DeltaPhase::ThinkingDelta,
    });
    match &out[0] {
        AgentEvent::Reasoning(s) => assert_eq!(s.as_str(), " about this"),
        _ => panic!("expected Reasoning"),
    }
}

/// `MessageUpdate` with text and reasoning interleaved tracks
/// them independently. Verifies the per-kind delta state.
#[test]
fn text_and_reasoning_tracked_independently() {
    let mut bridge = EventBridge::new();
    // Reasoning arrives first.
    let _ = bridge.translate(LoopEvent::MessageUpdate {
        message: AssistantMessage::new(
            vec![ContentBlock::Thinking {
                text: "thinking".to_string(),
            }],
            StopReason::Stop,
        ),
        phase: DeltaPhase::ThinkingStart,
    });
    // Then text arrives in a separate block.
    let out = bridge.translate(LoopEvent::MessageUpdate {
        message: AssistantMessage::new(
            vec![
                ContentBlock::Thinking {
                    text: "thinking".to_string(),
                },
                ContentBlock::Text {
                    text: "answer".to_string(),
                },
            ],
            StopReason::Stop,
        ),
        phase: DeltaPhase::TextStart,
    });
    // Token for "answer", not for the previously-emitted
    // "thinking".
    assert_eq!(out.len(), 1);
    match &out[0] {
        AgentEvent::Token(s) => assert_eq!(s.as_str(), "answer"),
        _ => panic!("expected Token"),
    }
}

/// `ToolExecutionStart` produces TWO events: `ToolCall` then
/// `ToolStarted`. Bridge also records the name for later
/// classification.
#[test]
fn tool_execution_start_emits_call_and_started() {
    let mut bridge = EventBridge::new();
    let out = bridge.translate(LoopEvent::ToolExecutionStart {
        tool_call_id: "call-1".to_string(),
        tool_name: "read".to_string(),
        args: serde_json::json!({"path": "/tmp/x"}),
    });
    assert_eq!(out.len(), 2);
    match &out[0] {
        AgentEvent::ToolCall { id, name, args } => {
            assert_eq!(id.as_str(), "call-1");
            assert_eq!(name.as_str(), "read");
            assert_eq!(args["path"], "/tmp/x");
        }
        _ => panic!("expected ToolCall"),
    }
    match &out[1] {
        AgentEvent::ToolStarted { id } => {
            assert_eq!(id.as_str(), "call-1");
        }
        _ => panic!("expected ToolStarted"),
    }
}

/// `ToolExecutionEnd` → `ToolResult` with `ToolContent::File`
/// for tool names that surface file refs.
#[test]
fn tool_execution_end_classifies_file_tools_as_file() {
    let mut bridge = EventBridge::new();
    // Record the tool name first.
    let _ = bridge.translate(LoopEvent::ToolExecutionStart {
        tool_call_id: "call-1".to_string(),
        tool_name: "read".to_string(),
        args: serde_json::json!({}),
    });
    let out = bridge.translate(LoopEvent::ToolExecutionEnd {
        tool_call_id: "call-1".to_string(),
        tool_name: "read".to_string(),
        result: LoopToolResult {
            content: vec![serde_json::json!({
                "type": "text",
                "text": "file contents here"
            })],
            details: serde_json::Value::Null,
            terminate: None,
        },
        is_error: false,
    });
    assert_eq!(out.len(), 1);
    match &out[0] {
        AgentEvent::ToolResult { id, output, kind } => {
            assert_eq!(id.as_str(), "call-1");
            assert_eq!(output.as_str(), "file contents here");
            assert!(matches!(kind, ToolContent::File));
        }
        _ => panic!("expected ToolResult"),
    }
}

/// `ToolExecutionEnd` → `ToolResult` with `ToolContent::Text`
/// for tools that aren't in the file-classification set.
#[test]
fn tool_execution_end_classifies_other_tools_as_text() {
    let mut bridge = EventBridge::new();
    let _ = bridge.translate(LoopEvent::ToolExecutionStart {
        tool_call_id: "call-2".to_string(),
        tool_name: "bash".to_string(),
        args: serde_json::json!({}),
    });
    let out = bridge.translate(LoopEvent::ToolExecutionEnd {
        tool_call_id: "call-2".to_string(),
        tool_name: "bash".to_string(),
        result: LoopToolResult {
            content: vec![serde_json::json!({"type": "text", "text": "stdout"})],
            details: serde_json::Value::Null,
            terminate: None,
        },
        is_error: false,
    });
    match &out[0] {
        AgentEvent::ToolResult { kind, .. } => {
            assert!(matches!(kind, ToolContent::Text));
        }
        _ => panic!("expected ToolResult"),
    }
}

/// `MessageStart` / `MessageEnd` are no-ops at the AgentEvent
/// boundary (dirge handles user-message rendering / done
/// `LoopMessage::Custom` flowing through `MessageStart` becomes
/// an `AgentEvent::CustomMessage` carrying the same JSON payload
/// — the bridge keeps the payload opaque so the UI's renderer
/// lookup gets the full structure.
#[test]
fn message_start_custom_emits_custom_message_event() {
    let mut bridge = EventBridge::new();
    let payload = serde_json::json!({"type": "status", "content": "hello"});
    let events = bridge.translate(LoopEvent::MessageStart {
        message: LoopMessage::Custom(payload.clone()),
    });
    assert_eq!(events.len(), 1);
    match &events[0] {
        AgentEvent::CustomMessage { payload: out } => assert_eq!(out, &payload),
        other => panic!("unexpected event: {other:?}"),
    }
}

/// `MessageStart` for User messages now emits `UserMessage`
/// so steering-injected user messages are displayed in the UI
/// log. ToolResult and Assistant MessageStart remain no-ops.
/// `MessageEnd` is always a no-op.
#[test]
fn message_start_end_behavior() {
    let mut bridge = EventBridge::new();
    let user_msg = LoopMessage::User(UserMessage {
        content: "hi".to_string(),
    });
    // User messages now emit UserMessage.
    let events = bridge.translate(LoopEvent::MessageStart {
        message: user_msg.clone(),
    });
    assert_eq!(events.len(), 1);
    match &events[0] {
        AgentEvent::UserMessage { content } => assert_eq!(content.as_str(), "hi"),
        other => panic!("expected UserMessage, got {other:?}"),
    }
    // MessageEnd is still a no-op for user messages.
    assert!(
        bridge
            .translate(LoopEvent::MessageEnd { message: user_msg })
            .is_empty()
    );

    // ToolResult MessageStart/End remain no-ops.
    let tool_msg = LoopMessage::ToolResult(ToolResultMessage {
        tool_call_id: "c1".to_string(),
        tool_name: "echo".to_string(),
        content: Vec::new(),
        details: serde_json::Value::Null,
        is_error: false,
    });
    assert!(
        bridge
            .translate(LoopEvent::MessageStart {
                message: tool_msg.clone()
            })
            .is_empty()
    );
    assert!(
        bridge
            .translate(LoopEvent::MessageEnd { message: tool_msg })
            .is_empty()
    );
}

/// `MessageUpdate` with End-phase markers are no-ops (the
/// content was already streamed via the corresponding Delta).
#[test]
fn message_update_end_phases_are_no_ops() {
    let mut bridge = EventBridge::new();
    for phase in [
        DeltaPhase::TextEnd,
        DeltaPhase::ThinkingEnd,
        DeltaPhase::ToolCallStart,
        DeltaPhase::ToolCallDelta,
        DeltaPhase::ToolCallEnd,
    ] {
        let out = bridge.translate(LoopEvent::MessageUpdate {
            message: assistant_with_text("any"),
            phase,
        });
        assert!(out.is_empty(), "phase {phase:?} should be no-op");
    }
}

/// dirge-5h5 layer-1 probe: drive 7 parallel `ToolExecutionStart`
/// events followed by their `ToolExecutionEnd` events through a
/// single bridge instance and assert every emitted `ToolResult`
/// carries non-empty `output`. Mirrors the production parallel-
/// execute ordering (all Starts first, then all Ends — possibly
/// out-of-source order). If the bridge ever drops, shadows, or
/// misroutes content for parallel reads, this test catches it.
#[test]
fn parallel_tool_execution_ends_preserve_distinct_content() {
    let mut bridge = EventBridge::new();
    let n = 7;
    // Phase 1: 7 Starts in source order (mirrors tools.rs:843-852).
    for i in 0..n {
        let evts = bridge.translate(LoopEvent::ToolExecutionStart {
            tool_call_id: format!("call-{i}"),
            tool_name: "read".to_string(),
            args: serde_json::json!({"path": format!("/f{i}")}),
        });
        // ToolCall + ToolStarted = 2 events.
        assert_eq!(evts.len(), 2, "start {i} should emit 2 events");
    }
    // Phase 2: Ends in REVERSE order (simulating completion-order
    // != source-order — what join_all surfaces in practice).
    for i in (0..n).rev() {
        let payload = format!("file-{i}-contents");
        let evts = bridge.translate(LoopEvent::ToolExecutionEnd {
            tool_call_id: format!("call-{i}"),
            tool_name: "read".to_string(),
            result: LoopToolResult {
                content: vec![serde_json::json!({"type": "text", "text": payload})],
                details: serde_json::Value::Null,
                terminate: None,
            },
            is_error: false,
        });
        assert_eq!(evts.len(), 1, "end {i} should emit 1 event");
        match &evts[0] {
            AgentEvent::ToolResult { id, output, kind } => {
                assert_eq!(id.as_str(), format!("call-{i}"));
                assert!(
                    !output.is_empty(),
                    "ToolResult for call-{i} had empty output"
                );
                assert_eq!(output.as_str(), format!("file-{i}-contents"));
                assert!(matches!(kind, ToolContent::File));
            }
            other => panic!("expected ToolResult, got {other:?}"),
        }
    }
}

/// dirge-5h5 layer-1 probe: an empty `content` Vec in
/// `LoopToolResult` flattens to an empty string. The bridge does
/// NOT synthesize a placeholder. Documents the only path by which
/// `AgentEvent::ToolResult.output` can be empty — and shows it
/// requires `content: vec![]` upstream, not anything the bridge
/// itself does.
#[test]
fn empty_loop_tool_result_content_produces_empty_output() {
    let mut bridge = EventBridge::new();
    let _ = bridge.translate(LoopEvent::ToolExecutionStart {
        tool_call_id: "c1".to_string(),
        tool_name: "read".to_string(),
        args: serde_json::json!({}),
    });
    let out = bridge.translate(LoopEvent::ToolExecutionEnd {
        tool_call_id: "c1".to_string(),
        tool_name: "read".to_string(),
        result: LoopToolResult {
            content: vec![],
            details: serde_json::Value::Null,
            terminate: None,
        },
        is_error: false,
    });
    match &out[0] {
        AgentEvent::ToolResult { output, .. } => {
            assert!(
                output.is_empty(),
                "empty content Vec must produce empty output (no synthesis)"
            );
        }
        _ => panic!("expected ToolResult"),
    }
}

/// `flatten_content` joins multiple text blocks with newlines.
/// Image / other-type blocks fall back to JSON stringify (not
/// dropped).
#[test]
fn flatten_content_joins_text_blocks() {
    let blocks = vec![
        serde_json::json!({"type": "text", "text": "line 1"}),
        serde_json::json!({"type": "text", "text": "line 2"}),
    ];
    assert_eq!(flatten_content(&blocks), "line 1\nline 2");
}

/// `flatten_content` falls back to JSON stringify for
/// non-text blocks. Preserves the data so consumers can
/// inspect.
#[test]
fn flatten_content_stringifies_unknown_blocks() {
    let blocks = vec![
        serde_json::json!({"type": "text", "text": "hello"}),
        serde_json::json!({"type": "image", "url": "https://example/x.png"}),
    ];
    let out = flatten_content(&blocks);
    assert!(out.contains("hello"));
    assert!(out.contains("image"));
}

/// Bridge can be reused across hand-crafted event sequences
/// without polluting state between unrelated calls — except
/// for the documented per-run state (turn_index, last_text).
/// This test confirms state is properly threaded through a
/// realistic full-run event sequence (TurnStart → tokens →
/// tool call → tool result → tokens → TurnEnd → AgentEnd).
#[test]
fn full_run_event_sequence_translates_correctly() {
    let mut bridge = EventBridge::new();
    let mut all = Vec::new();

    all.extend(bridge.translate(LoopEvent::AgentStart));
    all.extend(bridge.translate(LoopEvent::TurnStart));
    all.extend(bridge.translate(LoopEvent::MessageUpdate {
        message: assistant_with_text("Sure, "),
        phase: DeltaPhase::TextStart,
    }));
    all.extend(bridge.translate(LoopEvent::MessageUpdate {
        message: assistant_with_text("Sure, I'll help."),
        phase: DeltaPhase::TextDelta,
    }));
    all.extend(bridge.translate(LoopEvent::ToolExecutionStart {
        tool_call_id: "c1".to_string(),
        tool_name: "read".to_string(),
        args: serde_json::json!({"path": "/x"}),
    }));
    all.extend(bridge.translate(LoopEvent::ToolExecutionEnd {
        tool_call_id: "c1".to_string(),
        tool_name: "read".to_string(),
        result: LoopToolResult {
            content: vec![serde_json::json!({"type": "text", "text": "data"})],
            details: serde_json::Value::Null,
            terminate: None,
        },
        is_error: false,
    }));
    all.extend(bridge.translate(LoopEvent::TurnEnd {
        message: assistant_with_text("Sure, I'll help."),
        tool_results: Vec::new(),
    }));
    all.extend(bridge.translate(LoopEvent::AgentEnd {
        messages: vec![LoopMessage::Assistant(assistant_with_text(
            "final response",
        ))],
    }));

    // Expected sequence: TurnStart(0), Token("Sure, "),
    // Token("I'll help."), ToolCall, ToolStarted, ToolResult,
    // TurnEnd(0), Done.
    let kinds: Vec<_> = all
        .iter()
        .map(|e| match e {
            AgentEvent::Token(_) => "Token",
            AgentEvent::Reasoning(_) => "Reasoning",
            AgentEvent::ToolCall { .. } => "ToolCall",
            AgentEvent::ToolStarted { .. } => "ToolStarted",
            AgentEvent::ToolResult { .. } => "ToolResult",
            AgentEvent::TurnStart { .. } => "TurnStart",
            AgentEvent::TurnEnd { .. } => "TurnEnd",
            AgentEvent::Usage { .. } => "Usage",
            AgentEvent::Done { .. } => "Done",
            AgentEvent::Error(_) => "Error",
            AgentEvent::ContextOverflow { .. } => "ContextOverflow",
            AgentEvent::CompactionStarted { .. } => "CompactionStarted",
            AgentEvent::ContextCompacted { .. } => "ContextCompacted",
            AgentEvent::CheckpointRefresh { .. } => "CheckpointRefresh",
            AgentEvent::Interjected { .. } => "Interjected",
            AgentEvent::CustomMessage { .. } => "CustomMessage",
            AgentEvent::UserMessage { .. } => "UserMessage",
            AgentEvent::RetryNotice { .. } => "RetryNotice",
            AgentEvent::SystemNotice { .. } => "SystemNotice",
            AgentEvent::RepairStats { .. } => "RepairStats",
            AgentEvent::EscalationActivated { .. } => "EscalationActivated",
        })
        .collect();
    assert_eq!(
        kinds,
        vec![
            "TurnStart",
            "Token",
            "Token",
            "ToolCall",
            "ToolStarted",
            "ToolResult",
            "TurnEnd",
            "Done",
        ]
    );
}

/// `LoopEvent::Usage` translates to `AgentEvent::Usage`, carrying
/// the provider's real input/cached/creation counts through to the
/// host so it can fold them into the session cache stats.
#[test]
fn usage_event_translates_with_cache_counts() {
    use crate::agent::agent_loop::message::TokenUsage;
    let mut bridge = EventBridge::new();
    let out = bridge.translate(LoopEvent::Usage {
        usage: TokenUsage {
            input_tokens: 1000,
            output_tokens: 50,
            cached_input_tokens: 800,
            cache_creation_input_tokens: 0,
        },
    });
    assert_eq!(out.len(), 1);
    match &out[0] {
        AgentEvent::Usage {
            input_tokens,
            cached_input_tokens,
            cache_creation_input_tokens,
        } => {
            assert_eq!(*input_tokens, 1000);
            assert_eq!(*cached_input_tokens, 800);
            assert_eq!(*cache_creation_input_tokens, 0);
        }
        other => panic!("expected AgentEvent::Usage, got {other:?}"),
    }
}