tiycore 0.2.9

Unified LLM API and stateful Agent runtime in Rust
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
//! Tests for agent module: types, state, agent.

use serde_json::json;
use tiycore::agent::*;
use tiycore::thinking::ThinkingLevel;
use tiycore::types::*;

// ============================================================================
// AgentMessage tests
// ============================================================================

#[test]
fn test_agent_message_from_user() {
    let user = UserMessage::text("Hello");
    let agent_msg: AgentMessage = user.into();
    assert!(matches!(agent_msg, AgentMessage::User(_)));
}

#[test]
fn test_agent_message_from_assistant() {
    let assistant = AssistantMessage::builder()
        .api(Api::OpenAICompletions)
        .provider(Provider::OpenAI)
        .model("gpt-4o")
        .build()
        .unwrap();
    let agent_msg: AgentMessage = assistant.into();
    assert!(matches!(agent_msg, AgentMessage::Assistant(_)));
}

#[test]
fn test_agent_message_from_tool_result() {
    let tr = ToolResultMessage::text("call_1", "tool", "result", false);
    let agent_msg: AgentMessage = tr.into();
    assert!(matches!(agent_msg, AgentMessage::ToolResult(_)));
}

#[test]
fn test_agent_message_from_message() {
    let msg = Message::User(UserMessage::text("Hello"));
    let agent_msg: AgentMessage = msg.into();
    assert!(matches!(agent_msg, AgentMessage::User(_)));
}

#[test]
fn test_agent_message_to_message() {
    let agent_msg = AgentMessage::User(UserMessage::text("Hello"));
    let msg: Option<Message> = agent_msg.into();
    assert!(msg.is_some());
    assert!(msg.unwrap().is_user());
}

// ============================================================================
// AgentTool tests
// ============================================================================

#[test]
fn test_agent_tool_new() {
    let tool = AgentTool::new(
        "get_weather",
        "Get Weather",
        "Get weather for a location",
        json!({"type": "object", "properties": {"city": {"type": "string"}}}),
    );
    assert_eq!(tool.name, "get_weather");
    assert_eq!(tool.label, "Get Weather");
    assert_eq!(tool.description, "Get weather for a location");
}

#[test]
fn test_agent_tool_as_tool() {
    let agent_tool = AgentTool::new(
        "calc",
        "Calculator",
        "Perform calculations",
        json!({"type": "object"}),
    );
    let tool = agent_tool.as_tool();
    assert_eq!(tool.name, "calc");
    assert_eq!(tool.description, "Perform calculations");
}

#[test]
fn test_agent_tool_from_tool() {
    let tool = Tool::new("my_tool", "My description", json!({"type": "object"}));
    let agent_tool: AgentTool = tool.into();
    assert_eq!(agent_tool.name, "my_tool");
    assert_eq!(agent_tool.label, "my_tool"); // label defaults to name
    assert_eq!(agent_tool.description, "My description");
}

// ============================================================================
// AgentConfig tests
// ============================================================================

#[test]
fn test_agent_config_new() {
    let model = Model::builder()
        .id("gpt-4o")
        .name("GPT-4o")
        .api(Api::OpenAICompletions)
        .provider(Provider::OpenAI)
        .base_url("http://test")
        .context_window(128000)
        .max_tokens(16384)
        .build()
        .unwrap();

    let config = AgentConfig::new(model.clone());
    assert_eq!(config.model.id, "gpt-4o");
    assert_eq!(config.thinking_level, ThinkingLevel::Off);
    assert_eq!(config.tool_execution, ToolExecutionMode::Parallel);
}

// ============================================================================
// ToolExecutionMode tests
// ============================================================================

#[test]
fn test_tool_execution_mode_default_is_parallel() {
    assert_eq!(ToolExecutionMode::default(), ToolExecutionMode::Parallel);
}

// ============================================================================
// AgentToolResult tests
// ============================================================================

#[test]
fn test_agent_tool_result_text() {
    let result = AgentToolResult::text("Hello result");
    assert_eq!(result.content.len(), 1);
    assert!(result.content[0].is_text());
    assert!(result.details.is_none());
}

#[test]
fn test_agent_tool_result_error() {
    let result = AgentToolResult::error("Something failed");
    assert_eq!(result.content.len(), 1);
    assert!(result.content[0].is_text());
}

// ============================================================================
// AgentContext tests
// ============================================================================

#[test]
fn test_agent_context_default() {
    let ctx = AgentContext::default();
    assert_eq!(ctx.system_prompt, "");
    assert!(ctx.messages.is_empty());
    assert!(ctx.tools.is_none());
}

// ============================================================================
// AgentEvent tests
// ============================================================================

#[test]
fn test_agent_event_variants() {
    let _start = AgentEvent::AgentStart;
    let _end = AgentEvent::AgentEnd { messages: vec![] };
    let _turn_start = AgentEvent::TurnStart { turn_index: 0 };
    let _tool_start = AgentEvent::ToolExecutionStart {
        turn_index: 0,
        tool_call_id: "id".to_string(),
        tool_name: "tool".to_string(),
        args: json!({}),
    };
    let _tool_end = AgentEvent::ToolExecutionEnd {
        turn_index: 0,
        tool_call_id: "id".to_string(),
        tool_name: "tool".to_string(),
        result: json!({}),
        is_error: false,
    };
    // Just verifying these compile and can be created
}

// ============================================================================
// AgentState tests
// ============================================================================

#[test]
fn test_agent_state_new() {
    let state = AgentState::new();
    assert_eq!(*state.system_prompt.read(), "");
    assert!(state.messages.read().is_empty());
    assert!(!state.is_streaming());
    assert_eq!(state.message_count(), 0);
}

#[test]
fn test_agent_state_set_system_prompt() {
    let state = AgentState::new();
    state.set_system_prompt("You are helpful.");
    assert_eq!(*state.system_prompt.read(), "You are helpful.");
}

// test_agent_state_set_model removed: model now lives in AgentConfig only.

#[test]
fn test_agent_state_set_thinking_level() {
    // thinking_level now lives in AgentConfig; AgentState no longer stores it.
    // This test verifies it's accessible via Agent::snapshot().
    let agent = Agent::new();
    agent.set_thinking_level(ThinkingLevel::High);
    let snapshot = agent.snapshot();
    assert_eq!(snapshot.thinking_level, ThinkingLevel::High);
}

#[test]
fn test_agent_state_set_tools() {
    let state = AgentState::new();
    state.set_tools(vec![
        AgentTool::new("tool1", "Tool 1", "desc1", json!({})),
        AgentTool::new("tool2", "Tool 2", "desc2", json!({})),
    ]);
    assert_eq!(state.tools.read().len(), 2);
}

#[test]
fn test_agent_state_messages() {
    let state = AgentState::new();
    state.add_message(AgentMessage::User(UserMessage::text("Hello")));
    state.add_message(AgentMessage::User(UserMessage::text("World")));
    assert_eq!(state.message_count(), 2);

    state.replace_messages(vec![AgentMessage::User(UserMessage::text("New"))]);
    assert_eq!(state.message_count(), 1);

    state.clear_messages();
    assert_eq!(state.message_count(), 0);
}

#[test]
fn test_agent_state_streaming() {
    let state = AgentState::new();
    assert!(!state.is_streaming());
    state.set_streaming(true);
    assert!(state.is_streaming());
    state.set_streaming(false);
    assert!(!state.is_streaming());
}

#[test]
fn test_agent_state_reset() {
    let state = AgentState::new();
    state.set_system_prompt("test");
    state.add_message(AgentMessage::User(UserMessage::text("hello")));
    state.set_streaming(true);
    *state.error.write() = Some("err".to_string());

    state.reset();

    assert_eq!(*state.system_prompt.read(), "");
    assert!(state.messages.read().is_empty());
    assert!(!state.is_streaming());
    assert!(state.error.read().is_none());
}

#[test]
fn test_agent_state_clone() {
    let state = AgentState::new();
    state.set_system_prompt("test");
    state.add_message(AgentMessage::User(UserMessage::text("hello")));

    let cloned = state.clone();
    assert_eq!(*cloned.system_prompt.read(), "test");
    assert_eq!(cloned.message_count(), 1);

    // Modifying original doesn't affect clone
    state.set_system_prompt("modified");
    assert_eq!(*cloned.system_prompt.read(), "test");
}

// test_agent_state_with_model removed: model now lives in AgentConfig only.
// Use Agent::with_model() instead.

// ============================================================================
// Agent tests
// ============================================================================

#[test]
fn test_agent_new_defaults() {
    let agent = Agent::new();
    let state = agent.state();
    assert_eq!(*state.system_prompt.read(), "");
    // thinking_level now accessed via snapshot
    let snapshot = agent.snapshot();
    assert_eq!(snapshot.thinking_level, ThinkingLevel::Off);
    assert!(!state.is_streaming());
}

#[test]
fn test_agent_with_model() {
    let model = Model::builder()
        .id("claude-sonnet-4")
        .name("Claude Sonnet 4")
        .api(Api::AnthropicMessages)
        .provider(Provider::Anthropic)
        .base_url("https://api.anthropic.com/v1")
        .context_window(200000)
        .max_tokens(16000)
        .build()
        .unwrap();

    let agent = Agent::with_model(model);
    let snapshot = agent.snapshot();
    assert_eq!(snapshot.model.id, "claude-sonnet-4");
}

#[test]
fn test_agent_set_system_prompt() {
    let agent = Agent::new();
    agent.set_system_prompt("You are an AI.");
    assert_eq!(*agent.state().system_prompt.read(), "You are an AI.");
}

#[test]
fn test_agent_set_model() {
    let agent = Agent::new();
    let model = Model::builder()
        .id("new-model")
        .name("New")
        .api(Api::OpenAICompletions)
        .provider(Provider::OpenAI)
        .base_url("http://test")
        .context_window(4096)
        .max_tokens(1024)
        .build()
        .unwrap();

    agent.set_model(model);
    let snapshot = agent.snapshot();
    assert_eq!(snapshot.model.id, "new-model");
}

#[test]
fn test_agent_set_thinking_level() {
    let agent = Agent::new();
    agent.set_thinking_level(ThinkingLevel::High);
    let snapshot = agent.snapshot();
    assert_eq!(snapshot.thinking_level, ThinkingLevel::High);
}

#[test]
fn test_agent_set_tools() {
    let agent = Agent::new();
    agent.set_tools(vec![AgentTool::new("tool1", "Tool 1", "desc1", json!({}))]);
    assert_eq!(agent.state().tools.read().len(), 1);
}

#[test]
fn test_agent_messages_operations() {
    let agent = Agent::new();

    agent.append_message(AgentMessage::User(UserMessage::text("Hello")));
    assert_eq!(agent.state().message_count(), 1);

    agent.append_message(AgentMessage::User(UserMessage::text("World")));
    assert_eq!(agent.state().message_count(), 2);

    agent.replace_messages(vec![AgentMessage::User(UserMessage::text("New"))]);
    assert_eq!(agent.state().message_count(), 1);

    agent.clear_messages();
    assert_eq!(agent.state().message_count(), 0);
}

#[test]
fn test_agent_steering_queue() {
    let agent = Agent::new();

    assert!(!agent.has_queued_messages());

    agent.steer(AgentMessage::User(UserMessage::text("Interrupt")));
    assert!(agent.has_queued_messages());

    agent.clear_steering_queue();
    assert!(!agent.has_queued_messages());
}

#[test]
fn test_agent_follow_up_queue() {
    let agent = Agent::new();

    agent.follow_up(AgentMessage::User(UserMessage::text("Later")));
    assert!(agent.has_queued_messages());

    agent.clear_follow_up_queue();
    assert!(!agent.has_queued_messages());
}

#[test]
fn test_agent_clear_all_queues() {
    let agent = Agent::new();

    agent.steer(AgentMessage::User(UserMessage::text("Interrupt")));
    agent.follow_up(AgentMessage::User(UserMessage::text("Later")));
    assert!(agent.has_queued_messages());

    agent.clear_all_queues();
    assert!(!agent.has_queued_messages());
}

#[test]
fn test_agent_reset() {
    let agent = Agent::new();
    agent.set_system_prompt("test");
    agent.append_message(AgentMessage::User(UserMessage::text("hi")));
    agent.steer(AgentMessage::User(UserMessage::text("interrupt")));
    agent.follow_up(AgentMessage::User(UserMessage::text("later")));

    agent.reset();

    assert_eq!(*agent.state().system_prompt.read(), "");
    assert_eq!(agent.state().message_count(), 0);
    assert!(!agent.has_queued_messages());
}

#[test]
fn test_agent_abort() {
    let agent = Agent::new();
    agent.state().set_streaming(true);
    agent.steer(AgentMessage::User(UserMessage::text("x")));

    agent.abort();

    assert!(!agent.state().is_streaming());
    assert!(agent.has_queued_messages());
}

#[tokio::test]
async fn test_agent_prompt_basic() {
    // Without a provider registered, prompt should return a ProviderError
    let agent = Agent::new();
    let result = agent.prompt(UserMessage::text("Hello")).await;
    assert!(result.is_err());
    match result.unwrap_err() {
        AgentError::ProviderError(_) => {}
        other => panic!("Expected ProviderError, got {:?}", other),
    }
    // Even on error, streaming should be cleaned up
    assert!(!agent.state().is_streaming());
}

#[tokio::test]
async fn test_agent_prompt_already_streaming() {
    let agent = Agent::new();
    agent.state().set_streaming(true);

    let result = agent.prompt(UserMessage::text("Hello")).await;
    assert!(result.is_err());
    match result.unwrap_err() {
        AgentError::AlreadyStreaming => {}
        other => panic!("Expected AlreadyStreaming, got {:?}", other),
    }

    agent.state().set_streaming(false);
}

#[tokio::test]
async fn test_agent_continue_no_messages() {
    let agent = Agent::new();
    let result = agent.continue_().await;
    assert!(matches!(result, Err(AgentError::NoMessages)));
}

#[tokio::test]
async fn test_agent_continue_from_assistant() {
    let agent = Agent::new();
    let assistant = AssistantMessage::builder()
        .api(Api::OpenAICompletions)
        .provider(Provider::OpenAI)
        .model("gpt-4o")
        .build()
        .unwrap();
    agent.append_message(AgentMessage::Assistant(assistant));

    let result = agent.continue_().await;
    assert!(matches!(
        result,
        Err(AgentError::CannotContinueFromAssistant)
    ));
}

#[tokio::test]
async fn test_agent_continue_from_tool_result() {
    // Without a provider registered, continue_ should return a ProviderError
    let agent = Agent::new();
    agent.append_message(AgentMessage::ToolResult(ToolResultMessage::text(
        "call_1", "tool", "result", false,
    )));

    let result = agent.continue_().await;
    assert!(result.is_err());
    match result.unwrap_err() {
        AgentError::ProviderError(_) => {}
        other => panic!("Expected ProviderError, got {:?}", other),
    }
}

#[test]
fn test_agent_subscribe_and_emit() {
    use std::sync::{
        atomic::{AtomicUsize, Ordering},
        Arc,
    };

    let agent = Agent::new();
    let count = Arc::new(AtomicUsize::new(0));
    let count_clone = count.clone();

    let _unsub = agent.subscribe(move |_event| {
        count_clone.fetch_add(1, Ordering::SeqCst);
    });

    // Trigger events via prompt (which emits AgentStart then ProviderError → AgentEnd)
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(async {
        let _ = agent.prompt(UserMessage::text("hello")).await;
    });

    // Should have received AgentStart + AgentEnd even on error
    assert!(count.load(Ordering::SeqCst) >= 2);
}

// ============================================================================
// AgentError tests
// ============================================================================

#[test]
fn test_agent_error_display() {
    assert_eq!(
        format!("{}", AgentError::AlreadyStreaming),
        "Agent is already streaming"
    );
    assert_eq!(
        format!("{}", AgentError::NoMessages),
        "No messages in context"
    );
    assert_eq!(
        format!("{}", AgentError::CannotContinueFromAssistant),
        "Cannot continue from assistant message"
    );
    assert_eq!(
        format!("{}", AgentError::ToolNotFound("foo".into())),
        "Tool not found: foo"
    );
    assert_eq!(
        format!("{}", AgentError::ProviderError("bad".into())),
        "Provider error: bad"
    );
    assert_eq!(
        format!("{}", AgentError::MaxTurnsReached(25)),
        "Agent reached the maximum turn limit (25) before producing a final response"
    );
    assert_eq!(format!("{}", AgentError::Other("misc".into())), "misc");
}

// ============================================================================
// Promote follow-up → steering tests
// ============================================================================

#[test]
fn test_promote_follow_up_to_steering_success() {
    let agent = Agent::new();
    let handle = agent.follow_up(AgentMessage::from("hello"));
    assert_eq!(handle.kind, QueueKind::FollowUp);

    let new_handle = agent
        .promote_follow_up_to_steering(handle.id)
        .expect("promote should succeed");

    // New handle points to the steering queue
    assert_eq!(new_handle.kind, QueueKind::Steering);

    // Old follow-up handle is invalidated
    assert!(agent.cancel_follow_up_message(handle.id).is_none());

    // New steering handle is valid
    let msg = agent
        .cancel_steering_message(new_handle.id)
        .expect("should be able to cancel the promoted message");
    assert!(matches!(msg, AgentMessage::User(_)));
}

#[test]
fn test_promote_follow_up_not_found() {
    let agent = Agent::new();
    // Enqueue and immediately drain to get an id that no longer exists
    let handle = agent.follow_up(AgentMessage::from("temp"));
    agent.clear_follow_up_queue();
    assert!(agent.promote_follow_up_to_steering(handle.id).is_none());
}

#[test]
fn test_promote_follow_up_event_sequence() {
    use std::sync::{Arc, Mutex};

    let agent = Agent::new();
    let events: Arc<Mutex<Vec<QueueEvent>>> = Arc::new(Mutex::new(Vec::new()));
    let events_clone = events.clone();
    agent.set_on_queue_event(move |e| {
        events_clone.lock().unwrap().push(e);
    });

    let handle = agent.follow_up(AgentMessage::from("test"));
    // Clear events from the follow_up call
    events.lock().unwrap().clear();

    let _new_handle = agent.promote_follow_up_to_steering(handle.id).unwrap();

    let captured = events.lock().unwrap();
    // Expected: Removed(FollowUp), Transferred(FollowUp→Steering), Enqueued(Steering)
    assert_eq!(captured.len(), 3);

    assert!(matches!(
        &captured[0],
        QueueEvent::Removed { kind: QueueKind::FollowUp, count: 1, .. }
    ));
    assert!(matches!(
        &captured[1],
        QueueEvent::Transferred {
            from: QueueKind::FollowUp,
            to: QueueKind::Steering,
            count: 1,
            ..
        }
    ));
    assert!(matches!(
        &captured[2],
        QueueEvent::Enqueued { kind: QueueKind::Steering, count: 1, .. }
    ));
}

#[test]
fn test_try_promote_follow_up_queue_full() {
    let agent = Agent::new();
    // Set steering queue to max_depth=1, Reject
    agent.set_steering_backpressure(BackpressureConfig {
        max_depth: 1,
        overflow: OverflowBehavior::Reject,
    });
    // Fill the steering queue
    agent.steer(AgentMessage::from("blocking"));

    let handle = agent.follow_up(AgentMessage::from("promote-me"));

    let result = agent.try_promote_follow_up_to_steering(handle.id);
    assert!(result.is_err());
    match result.unwrap_err() {
        PromoteError::QueueFull(err) => {
            assert_eq!(err.max_depth, 1);
        }
        other => panic!("Expected QueueFull, got {:?}", other),
    }

    // Message should still be in the follow-up queue (can_push pre-check prevented removal)
    let msg = agent
        .cancel_follow_up_message(handle.id)
        .expect("message should still be in follow-up queue");
    assert!(matches!(msg, AgentMessage::User(_)));
}

#[test]
fn test_try_promote_follow_up_not_found() {
    let agent = Agent::new();
    // Enqueue and immediately drain to get an id that no longer exists
    let handle = agent.follow_up(AgentMessage::from("temp"));
    agent.clear_follow_up_queue();
    let result = agent.try_promote_follow_up_to_steering(handle.id);
    assert!(matches!(result, Err(PromoteError::NotFound)));
}

#[test]
fn test_promote_error_display() {
    let not_found = PromoteError::NotFound;
    assert_eq!(format!("{}", not_found), "message not found in follow-up queue");

    // Construct a QueueFullError via Agent-level try_steer (which calls try_push)
    let agent = Agent::new();
    agent.set_steering_backpressure(BackpressureConfig {
        max_depth: 1,
        overflow: OverflowBehavior::Reject,
    });
    agent.steer(AgentMessage::from("fill"));
    let err = agent.try_steer(AgentMessage::from("overflow")).unwrap_err();
    let queue_full = PromoteError::QueueFull(Box::new(err));
    assert!(format!("{}", queue_full).contains("steering queue full"));
}

#[test]
fn test_promote_multiple_messages_preserves_order() {
    let agent = Agent::new();

    let h1 = agent.follow_up(AgentMessage::from("first"));
    let h2 = agent.follow_up(AgentMessage::from("second"));

    // Promote the second one first
    let new_h2 = agent.promote_follow_up_to_steering(h2.id).unwrap();
    assert_eq!(new_h2.kind, QueueKind::Steering);

    // First should still be in follow-up
    let msg1 = agent.cancel_follow_up_message(h1.id).unwrap();
    assert!(matches!(msg1, AgentMessage::User(_)));
}

#[test]
fn test_queue_full_error_into_message() {
    // Construct a QueueFullError with a message via Agent try_steer
    let agent = Agent::new();
    agent.set_steering_backpressure(BackpressureConfig {
        max_depth: 1,
        overflow: OverflowBehavior::Reject,
    });
    agent.steer(AgentMessage::from("fill"));
    let err = agent.try_steer(AgentMessage::from("overflow")).unwrap_err();
    let msg = err.into_message().expect("should have a rejected message");
    assert!(matches!(msg, AgentMessage::User(_)));

    // can_push errors don't carry a message (not constructable from outside,
    // but we verify into_message returns None when message field is None
    // by checking a QueueFullError created without one)
    let err2 = QueueFullError::new(1, 1);
    assert!(err2.into_message().is_none());
}