embacle 0.29.1

LLM runner library — 12 AI CLI tools, ACP, and HTTP APIs (Gemini, Cohere, Groq, OpenRouter, OpenAI-compatible) as pluggable LLM providers with fallback chains, agent loop, guardrails, and cost tracking
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
// ABOUTME: End-to-end integration tests against real CLI binaries
// ABOUTME: Gated by EMBACLE_E2E_<RUNNER>=1 env vars; EMBACLE_E2E_ALL=1 runs everything
//
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 dravr.ai

#![allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::str_to_string
)]

use std::env;
use std::path::PathBuf;
use std::time::Duration;

use embacle::config::CliRunnerType;
use embacle::discovery::resolve_binary;
use embacle::types::{ChatMessage, ChatRequest, LlmProvider};
use embacle::RunnerConfig;
use tokio_stream::StreamExt;

/// Check whether a specific E2E runner test is enabled.
///
/// Returns `true` if `EMBACLE_E2E_ALL=1` or `EMBACLE_E2E_<TAG>=1`.
fn runner_enabled(tag: &str) -> bool {
    if env::var("EMBACLE_E2E_ALL").as_deref() == Ok("1") {
        return true;
    }
    let key = format!("EMBACLE_E2E_{}", tag.to_uppercase());
    env::var(&key).as_deref() == Ok("1")
}

/// Build a simple ping request that any LLM should handle.
fn ping_request() -> ChatRequest {
    ChatRequest::new(vec![
        ChatMessage::system("You are a test bot. Follow instructions exactly."),
        ChatMessage::user("Respond with exactly: PONG. Nothing else."),
    ])
    .with_max_tokens(20)
}

/// The streamed turn's instruction, worded as a verbatim reply.
///
/// Not "Count from 1 to 3": the Copilot CLI lists its skills in the system
/// prompt, and a 3B model once read "Count" as one and answered "I can't find
/// the \"counting\" skill". Every "Respond with exactly" prompt here held on
/// the same runners.
const STREAM_PROMPT: &str = "Respond with exactly these three lines and nothing else:\n1\n2\n3";

/// Build a streaming request.
fn stream_request() -> ChatRequest {
    ChatRequest::new(vec![ChatMessage::user(STREAM_PROMPT)]).with_max_tokens(30)
}

/// Standard timeout for E2E tests (CLI tools can be slow on first invocation).
const E2E_TIMEOUT: Duration = Duration::from_mins(5);

/// Resolve a binary or skip.
fn resolve_or_skip(runner_type: CliRunnerType) -> PathBuf {
    let env_override = env::var(runner_type.env_override_key()).ok();
    match resolve_binary(runner_type.binary_name(), env_override.as_deref()) {
        Ok(p) => p,
        Err(e) => {
            eprintln!(
                "  SKIP {}: binary not found: {e}",
                runner_type.binary_name()
            );
            // Return a dummy path — test will be skipped by the caller
            PathBuf::from("__not_found__")
        }
    }
}

// ============================================================================
// Shared test harness
// ============================================================================

/// Whether `haystack` carries every needle, in the order given.
fn contains_in_order(haystack: &str, needles: &[&str]) -> bool {
    let mut rest = haystack;
    for needle in needles {
        match rest.find(needle) {
            Some(at) => rest = &rest[at + needle.len()..],
            None => return false,
        }
    }
    true
}

/// Run the standard battery of tests against any `LlmProvider`.
async fn test_provider_complete(runner: &dyn LlmProvider) {
    let name = runner.name();

    // -- metadata --
    assert!(
        !runner.display_name().is_empty(),
        "{name}: display_name is empty"
    );
    assert!(
        !runner.default_model().is_empty(),
        "{name}: default_model is empty"
    );
    assert!(
        !runner.available_models().is_empty(),
        "{name}: available_models is empty"
    );

    // -- health check --
    let healthy = runner
        .health_check()
        .await
        .unwrap_or_else(|e| panic!("{name}: health_check failed: {e}"));
    assert!(healthy, "{name}: health_check returned false");

    // -- simple completion --
    let request = ping_request();
    let response = runner
        .complete(&request)
        .await
        .unwrap_or_else(|e| panic!("{name}: complete() failed: {e}"));

    eprintln!("  {name} complete: {:?}", response.content);
    assert!(
        response.content.to_uppercase().contains("PONG"),
        "{name}: the model answered the ping: {:?}",
        response.content
    );
    eprintln!("  {name} model:    {:?}", response.model);
    eprintln!("  {name} usage:    {:?}", response.usage);
}

/// Run streaming tests against a provider that supports it.
async fn test_provider_stream(runner: &dyn LlmProvider) {
    let name = runner.name();

    if !runner.capabilities().supports_streaming() {
        eprintln!("  {name}: streaming not supported, skipping stream test");
        return;
    }

    let request = stream_request();
    let mut stream = runner
        .complete_stream(&request)
        .await
        .unwrap_or_else(|e| panic!("{name}: complete_stream() failed: {e}"));

    let mut chunk_count: u32 = 0;
    let mut full_content = String::new();
    while let Some(result) = stream.next().await {
        let chunk = result.unwrap_or_else(|e| panic!("{name}: stream chunk error: {e}"));
        if !chunk.delta.is_empty() {
            full_content.push_str(&chunk.delta);
            chunk_count += 1;
        }
    }

    assert!(
        chunk_count > 0,
        "{name}: streaming produced 0 non-empty chunks"
    );
    eprintln!("  {name} stream: {chunk_count} chunks, content: {full_content:?}");
    assert!(
        contains_in_order(&full_content, &["1", "2", "3"]),
        "{name}: the stream spelled the count: {full_content:?}"
    );
}

// ============================================================================
// CLI Runner tests
// ============================================================================

#[tokio::test]
async fn e2e_claude_code() {
    if !runner_enabled("claude_code") {
        eprintln!("SKIP e2e_claude_code (set EMBACLE_E2E_CLAUDE_CODE=1)");
        return;
    }
    let path = resolve_or_skip(CliRunnerType::ClaudeCode);
    if !path.exists() {
        return;
    }
    let config = RunnerConfig::new(path).with_timeout(E2E_TIMEOUT);
    let runner = embacle::ClaudeCodeRunner::new(config);
    test_provider_complete(&runner).await;
    test_provider_stream(&runner).await;
}

#[tokio::test]
async fn e2e_copilot() {
    if !runner_enabled("copilot") {
        eprintln!("SKIP e2e_copilot (set EMBACLE_E2E_COPILOT=1)");
        return;
    }
    let path = resolve_or_skip(CliRunnerType::Copilot);
    if !path.exists() {
        return;
    }
    let config = RunnerConfig::new(path).with_timeout(E2E_TIMEOUT);
    let runner = embacle::CopilotRunner::new(config);
    test_provider_complete(&runner).await;
    test_provider_stream(&runner).await;
}

#[tokio::test]
async fn e2e_cursor_agent() {
    if !runner_enabled("cursor_agent") {
        eprintln!("SKIP e2e_cursor_agent (set EMBACLE_E2E_CURSOR_AGENT=1)");
        return;
    }
    let path = resolve_or_skip(CliRunnerType::CursorAgent);
    if !path.exists() {
        return;
    }
    let config = RunnerConfig::new(path).with_timeout(E2E_TIMEOUT);
    let runner = embacle::CursorAgentRunner::new(config);
    test_provider_complete(&runner).await;
    test_provider_stream(&runner).await;
}

#[tokio::test]
async fn e2e_opencode() {
    if !runner_enabled("opencode") {
        eprintln!("SKIP e2e_opencode (set EMBACLE_E2E_OPENCODE=1)");
        return;
    }
    let path = resolve_or_skip(CliRunnerType::OpenCode);
    if !path.exists() {
        return;
    }
    let config = RunnerConfig::new(path).with_timeout(E2E_TIMEOUT);
    let runner = embacle::OpenCodeRunner::new(config);
    test_provider_complete(&runner).await;
    test_provider_stream(&runner).await;
}

#[tokio::test]
async fn e2e_gemini_cli() {
    if !runner_enabled("gemini_cli") {
        eprintln!("SKIP e2e_gemini_cli (set EMBACLE_E2E_GEMINI_CLI=1)");
        return;
    }
    let path = resolve_or_skip(CliRunnerType::GeminiCli);
    if !path.exists() {
        return;
    }
    let config = RunnerConfig::new(path).with_timeout(E2E_TIMEOUT);
    let runner = embacle::GeminiCliRunner::new(config);
    test_provider_complete(&runner).await;
    test_provider_stream(&runner).await;
}

#[tokio::test]
async fn e2e_codex_cli() {
    if !runner_enabled("codex_cli") {
        eprintln!("SKIP e2e_codex_cli (set EMBACLE_E2E_CODEX_CLI=1)");
        return;
    }
    let path = resolve_or_skip(CliRunnerType::CodexCli);
    if !path.exists() {
        return;
    }
    let config = RunnerConfig::new(path).with_timeout(E2E_TIMEOUT);
    let runner = embacle::CodexCliRunner::new(config);
    test_provider_complete(&runner).await;
    test_provider_stream(&runner).await;
}

#[tokio::test]
async fn e2e_goose_cli() {
    if !runner_enabled("goose_cli") {
        eprintln!("SKIP e2e_goose_cli (set EMBACLE_E2E_GOOSE_CLI=1)");
        return;
    }
    let path = resolve_or_skip(CliRunnerType::GooseCli);
    if !path.exists() {
        return;
    }
    let config = RunnerConfig::new(path).with_timeout(E2E_TIMEOUT);
    let runner = embacle::GooseCliRunner::new(config);
    test_provider_complete(&runner).await;
    test_provider_stream(&runner).await;
}

#[tokio::test]
async fn e2e_cline_cli() {
    if !runner_enabled("cline_cli") {
        eprintln!("SKIP e2e_cline_cli (set EMBACLE_E2E_CLINE_CLI=1)");
        return;
    }
    let path = resolve_or_skip(CliRunnerType::ClineCli);
    if !path.exists() {
        return;
    }
    let config = RunnerConfig::new(path).with_timeout(E2E_TIMEOUT);
    let runner = embacle::ClineCliRunner::new(config);
    test_provider_complete(&runner).await;
    test_provider_stream(&runner).await;
}

#[tokio::test]
async fn e2e_continue_cli() {
    if !runner_enabled("continue_cli") {
        eprintln!("SKIP e2e_continue_cli (set EMBACLE_E2E_CONTINUE_CLI=1)");
        return;
    }
    let path = resolve_or_skip(CliRunnerType::ContinueCli);
    if !path.exists() {
        return;
    }
    let config = RunnerConfig::new(path).with_timeout(E2E_TIMEOUT);
    let runner = embacle::ContinueCliRunner::new(config);
    test_provider_complete(&runner).await;
    // Continue does not support streaming natively; complete_stream wraps complete
    test_provider_stream(&runner).await;
}

#[tokio::test]
async fn e2e_warp_cli() {
    if !runner_enabled("warp_cli") {
        eprintln!("SKIP e2e_warp_cli (set EMBACLE_E2E_WARP_CLI=1)");
        return;
    }
    let path = resolve_or_skip(CliRunnerType::WarpCli);
    if !path.exists() {
        return;
    }
    let config = RunnerConfig::new(path).with_timeout(E2E_TIMEOUT);
    let runner = embacle::WarpCliRunner::new(config);
    test_provider_complete(&runner).await;
    test_provider_stream(&runner).await;
}

#[tokio::test]
async fn e2e_kiro_cli() {
    if !runner_enabled("kiro_cli") {
        eprintln!("SKIP e2e_kiro_cli (set EMBACLE_E2E_KIRO_CLI=1)");
        return;
    }
    let path = resolve_or_skip(CliRunnerType::KiroCli);
    if !path.exists() {
        return;
    }
    let config = RunnerConfig::new(path).with_timeout(E2E_TIMEOUT);
    let runner = embacle::KiroCliRunner::new(config);
    test_provider_complete(&runner).await;
    // Kiro does not support streaming natively; complete_stream wraps complete
    test_provider_stream(&runner).await;
}

#[tokio::test]
async fn e2e_kilo_cli() {
    if !runner_enabled("kilo_cli") {
        eprintln!("SKIP e2e_kilo_cli (set EMBACLE_E2E_KILO_CLI=1)");
        return;
    }
    let path = resolve_or_skip(CliRunnerType::KiloCli);
    if !path.exists() {
        return;
    }
    let config = RunnerConfig::new(path).with_timeout(E2E_TIMEOUT);
    let runner = embacle::KiloCliRunner::new(config);
    test_provider_complete(&runner).await;
    test_provider_stream(&runner).await;
}

// ============================================================================
// Copilot Headless (ACP) tests — requires copilot-headless feature
// ============================================================================

#[cfg(feature = "copilot-headless")]
mod headless {
    use super::*;
    use embacle::CopilotHeadlessRunner;

    #[tokio::test]
    async fn e2e_copilot_headless_complete() {
        if !runner_enabled("copilot_headless") {
            eprintln!("SKIP e2e_copilot_headless_complete (set EMBACLE_E2E_COPILOT_HEADLESS=1)");
            return;
        }
        let runner = CopilotHeadlessRunner::from_env();
        test_provider_complete(&runner).await;
    }

    #[tokio::test]
    async fn e2e_copilot_headless_stream() {
        if !runner_enabled("copilot_headless") {
            eprintln!("SKIP e2e_copilot_headless_stream (set EMBACLE_E2E_COPILOT_HEADLESS=1)");
            return;
        }
        let runner = CopilotHeadlessRunner::from_env();
        test_provider_stream(&runner).await;
    }

    #[tokio::test]
    async fn e2e_copilot_headless_converse() {
        if !runner_enabled("copilot_headless") {
            eprintln!("SKIP e2e_copilot_headless_converse (set EMBACLE_E2E_COPILOT_HEADLESS=1)");
            return;
        }
        let runner = CopilotHeadlessRunner::from_env();

        let request = ChatRequest::new(vec![
            ChatMessage::system("You are a test bot. Follow instructions exactly."),
            ChatMessage::user("Respond with exactly: CONVERSE_OK. Nothing else."),
        ])
        .with_max_tokens(20);

        let response = runner
            .converse(&request)
            .await
            .unwrap_or_else(|e| panic!("converse() failed: {e}"));

        eprintln!("  headless converse content: {:?}", response.content);
        assert!(
            response.content.contains("CONVERSE_OK"),
            "copilot_headless converse: the model answered as told: {:?}",
            response.content
        );
        eprintln!("  headless converse model:   {:?}", response.model);
        eprintln!("  headless converse usage:   {:?}", response.usage);
        eprintln!(
            "  headless converse tools:   {} observed",
            response.tool_calls.len()
        );
        for tc in &response.tool_calls {
            eprintln!("    tool: {} ({})", tc.title, tc.status);
        }
        eprintln!("  headless converse finish:  {:?}", response.finish_reason);
    }

    #[tokio::test]
    async fn e2e_copilot_headless_converse_with_tools() {
        if !runner_enabled("copilot_headless") {
            eprintln!(
                "SKIP e2e_copilot_headless_converse_with_tools (set EMBACLE_E2E_COPILOT_HEADLESS=1)"
            );
            return;
        }
        let runner = CopilotHeadlessRunner::from_env();

        // Ask something that should trigger tool use (file read)
        let request = ChatRequest::new(vec![ChatMessage::user(
            "Read the file Cargo.toml in the current directory and tell me the package name.",
        )])
        .with_max_tokens(100);

        let response = runner
            .converse(&request)
            .await
            .unwrap_or_else(|e| panic!("converse() with tools failed: {e}"));

        assert!(
            !response.content.is_empty(),
            "copilot_headless converse_with_tools: empty content"
        );
        eprintln!(
            "  headless tools content: {:?}",
            &response.content[..response.content.len().min(200)]
        );
        eprintln!(
            "  headless tools observed: {} tool calls",
            response.tool_calls.len()
        );
        for tc in &response.tool_calls {
            eprintln!("    tool: {} [{}] ({})", tc.title, tc.id, tc.status);
        }
        assert!(
            !response.tool_calls.is_empty(),
            "reading a file is a tool call, and it is observed: {:?}",
            response.content
        );
    }
}

// ============================================================================
// OpenAI API Runner (requires `openai-api` feature + live API key)
// ============================================================================

/// E2E test for `OpenAiApiRunner` against a live `OpenAI`-compatible endpoint.
///
/// Enable with: `EMBACLE_E2E_OPENAI_API=1`
///
/// Required env vars (example for Groq):
///   `OPENAI_API_BASE_URL=https://api.groq.com/openai/v1`
///   `OPENAI_API_KEY=gsk_...`
///   `OPENAI_API_MODEL=llama-3.3-70b-versatile`
#[cfg(feature = "openai-api")]
mod openai_api_e2e {
    use super::*;
    use embacle::{OpenAiApiConfig, OpenAiApiRunner};

    #[tokio::test]
    async fn openai_api_complete_and_stream() {
        if !runner_enabled("openai_api") {
            eprintln!("  SKIP openai_api: set EMBACLE_E2E_OPENAI_API=1 to enable");
            return;
        }

        let config = OpenAiApiConfig::from_env();
        eprintln!(
            "  openai_api: base_url={}, model={}",
            config.base_url, config.model
        );

        let runner = OpenAiApiRunner::new(config).await;
        test_provider_complete(&runner).await;
    }
}

// ============================================================================
// Copilot SDK tests — requires copilot-sdk feature, a runtime pair on disk
// (COPILOT_RUNTIME_PATH) and Copilot auth (COPILOT_GITHUB_TOKEN)
// ============================================================================

#[cfg(feature = "copilot-sdk")]
mod sdk {
    use super::*;
    use embacle::types::ErrorKind;
    use embacle::{CopilotSdkRunner, HeadlessStreamEvent, HeadlessTurnProvider};

    const CODEWORD: &str = "DRAVR-COACH-7741";

    /// The model the live turns run on: `COPILOT_SDK_MODEL`, which a lane
    /// routing to its own provider sets to a model that provider serves, or a
    /// cheap catalogued Copilot model.
    fn model() -> String {
        env::var("COPILOT_SDK_MODEL").unwrap_or_else(|_| "claude-haiku-4.5".to_owned())
    }

    /// An id no catalogue and no provider serves.
    const UNKNOWN_MODEL: &str = "embacle-e2e-no-such-model";

    fn skipped(test: &str) -> bool {
        if runner_enabled("copilot_sdk") {
            return false;
        }
        eprintln!("SKIP {test} (set EMBACLE_E2E_COPILOT_SDK=1 and COPILOT_RUNTIME_PATH)");
        true
    }

    fn request(system: &str, user: &str) -> ChatRequest {
        let mut request =
            ChatRequest::new(vec![ChatMessage::system(system), ChatMessage::user(user)])
                .with_max_tokens(60);
        request.model = Some(model());
        request
    }

    #[tokio::test]
    async fn e2e_copilot_sdk_complete() {
        if skipped("e2e_copilot_sdk_complete") {
            return;
        }
        let runner = CopilotSdkRunner::from_env();
        test_provider_complete(&runner).await;
    }

    #[tokio::test]
    async fn e2e_copilot_sdk_stream() {
        if skipped("e2e_copilot_sdk_stream") {
            return;
        }
        let runner = CopilotSdkRunner::from_env();
        test_provider_stream(&runner).await;
    }

    /// The system prompt travels in the runtime's own system slot and
    /// replaces the Copilot CLI persona: the model answers from these
    /// instructions, and when asked to quote them it quotes ours.
    #[tokio::test]
    async fn e2e_copilot_sdk_system_prompt_replaces_the_copilot_persona() {
        if skipped("e2e_copilot_sdk_system_prompt_replaces_the_copilot_persona") {
            return;
        }
        let runner = CopilotSdkRunner::from_env();

        let codeword_turn = request(
            &format!(
                "You are Dravr's coach. Your codeword is {CODEWORD}. When asked for \
                 your codeword, reply with exactly the codeword and nothing else."
            ),
            "What is your codeword?",
        );
        let response = runner
            .converse(&codeword_turn)
            .await
            .unwrap_or_else(|e| panic!("converse() failed: {e}"));
        assert!(
            response.content.contains(CODEWORD),
            "the system prompt reached the model: {:?}",
            response.content
        );

        let quote_turn = request(
            "You are Dravr's coach and nothing else. When asked to quote your \
             instructions, reply with their first sentence, verbatim.",
            "Quote the first sentence of your instructions.",
        );
        let response = runner
            .converse(&quote_turn)
            .await
            .unwrap_or_else(|e| panic!("converse() failed: {e}"));
        let quoted = response.content.to_lowercase();
        assert!(
            quoted.contains("dravr"),
            "the model quotes our instructions: {:?}",
            response.content
        );
        assert!(
            !quoted.contains("github copilot"),
            "the Copilot CLI persona was displaced: {:?}",
            response.content
        );
    }

    /// The served model and the runtime's cache counts come back on every turn.
    #[tokio::test]
    async fn e2e_copilot_sdk_reports_the_served_model_and_cache_counts() {
        if skipped("e2e_copilot_sdk_reports_the_served_model_and_cache_counts") {
            return;
        }
        let runner = CopilotSdkRunner::from_env();
        let turn = request(
            "You are a test bot. Follow instructions exactly.",
            "Respond with exactly: PONG. Nothing else.",
        );
        let response = runner
            .converse(&turn)
            .await
            .unwrap_or_else(|e| panic!("converse() failed: {e}"));
        assert_eq!(
            response.model,
            model(),
            "the model that served is the one asked for"
        );
        let usage = response.usage.expect("assistant.usage was reported");
        assert!(usage.prompt_tokens > 0, "prompt tokens counted: {usage:?}");
        assert!(
            usage.completion_tokens > 0,
            "completion tokens counted: {usage:?}"
        );
        assert!(
            usage.cached_read_tokens.is_some() && usage.cached_write_tokens.is_some(),
            "cache counts are carried, not dropped: {usage:?}"
        );
        assert!(
            response.tool_calls.is_empty(),
            "no tools were offered, none ran"
        );
    }

    /// A model nothing serves fails loudly instead of being served by
    /// whatever the runtime substitutes — refused by the catalogue check under
    /// Copilot's routing, and by the endpoint itself under a provider.
    #[tokio::test]
    async fn e2e_copilot_sdk_unknown_model_fails_loudly() {
        if skipped("e2e_copilot_sdk_unknown_model_fails_loudly") {
            return;
        }
        let runner = CopilotSdkRunner::from_env();
        let mut turn = request("You are a test bot.", "Respond with exactly: PONG.");
        turn.model = Some(UNKNOWN_MODEL.to_owned());
        let err = runner
            .converse(&turn)
            .await
            .expect_err("an unknown model id is refused");
        assert_eq!(err.kind, ErrorKind::ModelUnavailable, "{err}");
        assert!(err.to_string().contains(UNKNOWN_MODEL), "{err}");
    }

    /// Streaming delivers text deltas and ends with the aggregated response.
    #[tokio::test]
    async fn e2e_copilot_sdk_converse_stream_ends_with_done() {
        if skipped("e2e_copilot_sdk_converse_stream_ends_with_done") {
            return;
        }
        let runner = CopilotSdkRunner::from_env();
        let turn = request(
            "You are a test bot. Follow instructions exactly.",
            STREAM_PROMPT,
        );
        let mut stream = runner
            .converse_stream(&turn)
            .await
            .unwrap_or_else(|e| panic!("converse_stream() failed: {e}"));

        let mut deltas = 0u32;
        let mut streamed = String::new();
        let mut done = None;
        while let Some(event) = stream.next().await {
            match event.unwrap_or_else(|e| panic!("stream error: {e}")) {
                HeadlessStreamEvent::TextDelta(delta) => {
                    deltas += 1;
                    streamed.push_str(&delta);
                }
                HeadlessStreamEvent::ToolCall(call) => panic!("no tool was offered: {call:?}"),
                HeadlessStreamEvent::Done(response) => done = Some(response),
            }
        }
        let done = done.expect("the stream ends with Done");
        assert!(deltas > 0, "text arrived incrementally");
        assert_eq!(
            done.content, streamed,
            "Done carries the text the deltas spelled"
        );
        assert_eq!(done.model, model());
        assert!(done.usage.is_some(), "usage rides on Done");
    }
}