outrig-cli 0.1.0

Command-line tool for running LLM agents with podman-isolated MCP servers.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
//! End-to-end smoke for `outrig mcp`. Gated behind `--features e2e`.
//!
//! Spawns the binary with `mcp` against a fixture repo whose config has one
//! backing MCP (`mcp-server-filesystem`) and *no* agent / providers /
//! models. Drives the spawned child as an MCP client over its stdio using
//! rmcp's [`serve_client`], then asserts on:
//!
//! - `tools/list` returns the namespaced union (`fs__*`) matching what
//!   `outrig run`'s banner reports.
//! - `tools/call fs__list_directory` returns the planted `HELLO.txt`.
//! - Closing the client side (drops stdin → child sees EOF) drives a clean
//!   teardown and exit-zero from the child.
//! - The child's container is reaped (no leftovers in `podman ps`).
//!
//! Run with:
//!
//! ```sh
//! cargo test -p outrig-cli --features e2e mcp_subcommand_smoke -- --nocapture
//! ```

#![cfg(feature = "e2e")]

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime};

use outrig::McpClient;
use outrig::config::{ImageConfig, McpServerSpec};
use outrig::container::{Container, ContainerLaunchSpec};
use outrig::image::{self, ImageTag};
use outrig_cli::session::{Session, SessionId, SessionStore};
use rmcp::model::CallToolRequestParams;
use rmcp::service::serve_client;
use serde_json::Value;
use tokio::process::Command;
use tokio::time::{sleep, timeout};

mod common;
use common::stream_lines;

const TEST_TIMEOUT: Duration = Duration::from_secs(120);

fn fixture_mcp_fs_dir() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .expect("outrig-cli is under crates/")
        .join("outrig/tests/fixtures/mcp-fs")
}

fn fs_spec() -> McpServerSpec {
    McpServerSpec::Short(vec![
        "mcp-server-filesystem".to_string(),
        "/workspace".to_string(),
    ])
}

async fn ensure_fixture_image() -> ImageTag {
    let cfg = ImageConfig {
        image_name: None,
        dockerfile: Some("Dockerfile".into()),
        context: Some(".".into()),
        build_args: BTreeMap::new(),
        security: Default::default(),
        mcp: BTreeMap::new(),
    };
    image::ensure_image(&cfg, &fixture_mcp_fs_dir(), false)
        .await
        .expect("ensure mcp-fs fixture image")
        .tag
}

fn write_mcp_config(repo: &Path) {
    let agents_dir = repo.join(".agents/outrig");
    std::fs::create_dir_all(&agents_dir).expect("mkdir .agents/outrig");

    let dockerfile = fixture_mcp_fs_dir().join("Dockerfile");
    let context = fixture_mcp_fs_dir();
    let config_toml = format!(
        r#"
default-image = "smoke"

[images.smoke]
dockerfile = "{dockerfile}"
context = "{context}"

  [images.smoke.mcp]
  fs = ["mcp-server-filesystem", "/workspace"]
"#,
        dockerfile = dockerfile.display(),
        context = context.display(),
    );
    std::fs::write(agents_dir.join("config.toml"), config_toml).expect("write config");
}

async fn start_fixture_container(image: &ImageTag, repo: &Path) -> Container {
    let mut container = Container::start(
        image,
        ContainerLaunchSpec::workspace(repo, Path::new("/workspace")),
    )
    .await
    .expect("start fixture container");
    container.bootstrap_user().await.expect("bootstrap user");
    container
}

fn create_host_session(
    session_root: &Path,
    repo: &Path,
    container: &Container,
    image: &ImageTag,
) -> SessionId {
    let store = SessionStore::new(session_root.to_path_buf());
    let sid = SessionId(container.session_suffix().to_string());
    let mut session = Session {
        id: sid.clone(),
        started_at: SystemTime::now(),
        ended_at: None,
        container_name: container.name().to_string(),
        image_tag: image.to_string(),
        image_config_name: "smoke".to_string(),
        agent_name: Some("smoke".to_string()),
        working_dir: repo.to_path_buf(),
        session_dir: PathBuf::new(),
        exit_code: None,
        link_target: None,
    };
    store
        .create(&sid, None, &mut session)
        .expect("host session");
    sid
}

struct McpRun {
    status: std::process::ExitStatus,
    stderr: String,
}

async fn run_mcp_client(args: &[String], repo: &Path) -> McpRun {
    let bin = env!("CARGO_BIN_EXE_outrig");
    let mut child = Command::new(bin)
        .args(args)
        .current_dir(repo)
        .env("OUTRIG_LOG", "info")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .kill_on_drop(true)
        .spawn()
        .expect("spawn outrig mcp");

    let child_stdin = child.stdin.take().expect("stdin piped");
    let child_stdout = child.stdout.take().expect("stdout piped");
    let stderr = child.stderr.take().expect("stderr piped");
    let stderr_buf = Arc::new(Mutex::new(String::new()));
    let stderr_task = tokio::spawn(stream_lines(stderr, stderr_buf.clone(), "stderr"));

    let service = serve_client((), (child_stdout, child_stdin))
        .await
        .expect("serve_client (initialize handshake)");
    let listing = service
        .list_tools(Default::default())
        .await
        .expect("tools/list");
    let names: Vec<String> = listing
        .tools
        .iter()
        .map(|t| t.name.as_ref().to_string())
        .collect();
    assert!(
        names.iter().any(|n| n == "fs__list_directory"),
        "expected `fs__list_directory` in {names:?}"
    );

    let call_args = serde_json::json!({"path": "/workspace"})
        .as_object()
        .unwrap()
        .clone();
    let call = service
        .call_tool(
            CallToolRequestParams::new("fs__list_directory".to_string()).with_arguments(call_args),
        )
        .await
        .expect("tools/call fs__list_directory");
    assert!(
        call.is_error != Some(true),
        "fs__list_directory should not be an error: {call:?}"
    );

    let _ = service.cancel().await;

    let status = timeout(TEST_TIMEOUT, child.wait())
        .await
        .unwrap_or_else(|_| panic!("subprocess did not exit within {TEST_TIMEOUT:?}"))
        .expect("child.wait");
    let _ = stderr_task.await;
    let stderr = stderr_buf.lock().unwrap().clone();
    eprintln!("--- subprocess stderr ---\n{stderr}");
    McpRun { status, stderr }
}

fn stderr_value<'a>(stderr: &'a str, prefix: &str) -> &'a str {
    stderr
        .lines()
        .find_map(|line| line.strip_prefix(prefix).map(str::trim))
        .unwrap_or_else(|| panic!("stderr lacked {prefix:?}: {stderr}"))
}

async fn wait_for_stderr_value(stderr: Arc<Mutex<String>>, prefix: &str) -> String {
    timeout(TEST_TIMEOUT, async {
        loop {
            {
                let snapshot = stderr.lock().unwrap().clone();
                if let Some(value) = snapshot
                    .lines()
                    .find_map(|line| line.strip_prefix(prefix).map(str::trim))
                {
                    return value.to_string();
                }
            }
            sleep(Duration::from_millis(50)).await;
        }
    })
    .await
    .unwrap_or_else(|_| panic!("stderr lacked {prefix:?}: {}", stderr.lock().unwrap()))
}

async fn initialize_http_session(client: &reqwest::Client, url: &str, id: u64) -> String {
    let response = client
        .post(url)
        .header("Accept", "application/json, text/event-stream")
        .header("Content-Type", "application/json")
        .json(&serde_json::json!({
            "jsonrpc": "2.0",
            "id": id,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "capabilities": {},
                "clientInfo": {
                    "name": "outrig-test",
                    "version": "1.0.0"
                }
            }
        }))
        .send()
        .await
        .expect("POST initialize");
    assert!(
        response.status().is_success(),
        "initialize failed with status {}",
        response.status()
    );
    let session_id = response
        .headers()
        .get("mcp-session-id")
        .expect("mcp-session-id header")
        .to_str()
        .expect("session id utf-8")
        .to_string();
    let _ = response.text().await.expect("initialize body");

    let response = client
        .post(url)
        .header("Accept", "application/json, text/event-stream")
        .header("Content-Type", "application/json")
        .header("mcp-session-id", &session_id)
        .header("Mcp-Protocol-Version", "2025-06-18")
        .json(&serde_json::json!({
            "jsonrpc": "2.0",
            "method": "notifications/initialized"
        }))
        .send()
        .await
        .expect("POST notifications/initialized");
    assert_eq!(
        response.status(),
        reqwest::StatusCode::ACCEPTED,
        "initialized notification should be accepted"
    );

    session_id
}

async fn post_http_mcp(
    client: &reqwest::Client,
    url: &str,
    session_id: &str,
    id: u64,
    method: &str,
    params: Value,
) -> Value {
    let response = client
        .post(url)
        .header("Accept", "application/json, text/event-stream")
        .header("Content-Type", "application/json")
        .header("mcp-session-id", session_id)
        .header("Mcp-Protocol-Version", "2025-06-18")
        .json(&serde_json::json!({
            "jsonrpc": "2.0",
            "id": id,
            "method": method,
            "params": params
        }))
        .send()
        .await
        .unwrap_or_else(|e| panic!("POST {method}: {e}"));
    assert!(
        response.status().is_success(),
        "{method} failed with status {}",
        response.status()
    );
    let body = response.text().await.expect("response body");
    json_rpc_response(&body, id)
}

fn json_rpc_response(body: &str, id: u64) -> Value {
    if let Ok(value) = serde_json::from_str::<Value>(body)
        && value.get("id").and_then(Value::as_u64) == Some(id)
    {
        return value;
    }

    for event in body.split("\n\n") {
        let data = event
            .lines()
            .filter_map(|line| line.trim().strip_prefix("data:"))
            .map(str::trim_start)
            .collect::<Vec<_>>()
            .join("\n");
        if data.trim().is_empty() {
            continue;
        }
        if let Ok(value) = serde_json::from_str::<Value>(data.trim())
            && value.get("id").and_then(Value::as_u64) == Some(id)
        {
            return value;
        }
    }

    panic!("no JSON-RPC response id {id} in body: {body}");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn mcp_subcommand_serves_namespaced_tools_and_exits_clean() {
    let _ = tracing_subscriber::fmt()
        .with_max_level(tracing::Level::INFO)
        .with_writer(std::io::stderr)
        .with_ansi(false)
        .try_init();

    // 1. Build the fixture repo: container-only config, no agent surface.
    let repo_dir = tempfile::tempdir().expect("tempdir repo");
    write_mcp_config(repo_dir.path());

    // The default workspace mount is `repo_root -> /workspace`; planting the
    // file at the repo root makes it visible inside the container.
    std::fs::write(repo_dir.path().join("HELLO.txt"), "hi\n").expect("write HELLO.txt");

    // 2. Spawn `outrig mcp` with all three pipes piped.
    let bin = env!("CARGO_BIN_EXE_outrig");
    let mut child = Command::new(bin)
        .arg("mcp")
        .current_dir(repo_dir.path())
        .env("OUTRIG_LOG", "info")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .kill_on_drop(true)
        .spawn()
        .expect("spawn outrig mcp");

    let child_stdin = child.stdin.take().expect("stdin piped");
    let child_stdout = child.stdout.take().expect("stdout piped");
    let stderr = child.stderr.take().expect("stderr piped");

    // Stream stderr line-by-line so a hang dumps everything-so-far.
    let stderr_buf = Arc::new(Mutex::new(String::new()));
    let stderr_task = tokio::spawn(stream_lines(stderr, stderr_buf.clone(), "stderr"));

    // 3. Drive the child as an MCP client over its stdio.
    let work = async {
        let service = serve_client((), (child_stdout, child_stdin))
            .await
            .expect("serve_client (initialize handshake)");

        let listing = service
            .list_tools(Default::default())
            .await
            .expect("tools/list");
        let names: Vec<String> = listing
            .tools
            .iter()
            .map(|t| t.name.as_ref().to_string())
            .collect();
        assert!(
            names.iter().any(|n| n == "fs__list_directory"),
            "expected `fs__list_directory` in {names:?}"
        );
        assert!(
            names.iter().any(|n| n == "fs__read_file"),
            "expected `fs__read_file` in {names:?}"
        );
        assert!(
            names.iter().all(|n| n.starts_with("fs__")),
            "every tool should be namespaced under `fs__`, got {names:?}"
        );

        let call_args = serde_json::json!({"path": "/workspace"})
            .as_object()
            .unwrap()
            .clone();
        let call = service
            .call_tool(
                CallToolRequestParams::new("fs__list_directory".to_string())
                    .with_arguments(call_args),
            )
            .await
            .expect("tools/call fs__list_directory");
        assert!(
            call.is_error != Some(true),
            "fs__list_directory should not be an error: {call:?}"
        );
        let body = call
            .content
            .iter()
            .filter_map(|c| match &c.raw {
                rmcp::model::RawContent::Text(t) => Some(t.text.as_str()),
                _ => None,
            })
            .collect::<Vec<_>>()
            .join("\n");
        assert!(
            body.contains("HELLO.txt"),
            "list_directory body should mention HELLO.txt, got: {body}"
        );

        // Cancel the rmcp client: closes the write side of child's stdin
        // and quiesces the dispatcher. Child's `serve_server` then sees
        // EOF, teardown runs, child exits 0.
        let _ = service.cancel().await;
    };

    // 4. Wait for everything with a wall-clock bound.
    timeout(TEST_TIMEOUT, work)
        .await
        .unwrap_or_else(|_| panic!("MCP work did not finish within {TEST_TIMEOUT:?}"));

    let wait_result = timeout(TEST_TIMEOUT, child.wait()).await;
    let status = match wait_result {
        Ok(Ok(s)) => s,
        Ok(Err(e)) => panic!("child.wait() failed: {e}"),
        Err(_) => {
            eprintln!(
                "--- subprocess stderr (before timeout kill) ---\n{}",
                stderr_buf.lock().unwrap()
            );
            let _ = child.kill().await;
            panic!("subprocess did not exit within {TEST_TIMEOUT:?}");
        }
    };
    let _ = stderr_task.await;

    let stderr_str = stderr_buf.lock().unwrap().clone();
    eprintln!("--- subprocess stderr ---\n{stderr_str}");

    assert!(
        status.success(),
        "outrig mcp exited with {status:?}; stderr was: {stderr_str}"
    );
    assert!(
        stderr_str.contains("[outrig] mcp fs:"),
        "stderr lacked per-server banner line: {stderr_str}"
    );
    assert!(
        stderr_str.contains("[outrig] transport: stdio"),
        "stderr lacked `transport: stdio` line: {stderr_str}"
    );
    assert!(
        stderr_str.contains("[outrig] mcp server ready"),
        "stderr lacked `mcp server ready` line: {stderr_str}"
    );

    // Verify our specific container was cleaned up.
    let session_line = stderr_str
        .lines()
        .find(|l| l.contains("[outrig] container started:"))
        .expect("banner must include `container started` line");
    let our_container = session_line
        .split("started:")
        .nth(1)
        .expect("container name after `started:`")
        .trim();
    let ps = Command::new("podman")
        .args(["ps", "-a", "--filter"])
        .arg(format!("name={our_container}"))
        .args(["--format", "{{.Names}}"])
        .output()
        .await
        .expect("podman ps");
    let leftovers = String::from_utf8_lossy(&ps.stdout);
    assert!(
        leftovers.trim().is_empty(),
        "this run's container `{our_container}` is still alive: {leftovers}"
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn mcp_listen_http_serves_multiple_independent_sessions() {
    let _ = tracing_subscriber::fmt()
        .with_max_level(tracing::Level::INFO)
        .with_writer(std::io::stderr)
        .with_ansi(false)
        .try_init();

    let repo_dir = tempfile::tempdir().expect("tempdir repo");
    write_mcp_config(repo_dir.path());
    std::fs::write(repo_dir.path().join("HELLO.txt"), "hi\n").expect("write HELLO.txt");

    let bin = env!("CARGO_BIN_EXE_outrig");
    let mut child = Command::new(bin)
        .args(["mcp", "--listen", "127.0.0.1:0"])
        .current_dir(repo_dir.path())
        .env("OUTRIG_LOG", "info")
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::piped())
        .kill_on_drop(true)
        .spawn()
        .expect("spawn outrig mcp --listen");

    let stderr = child.stderr.take().expect("stderr piped");
    let stderr_buf = Arc::new(Mutex::new(String::new()));
    let stderr_task = tokio::spawn(stream_lines(stderr, stderr_buf.clone(), "stderr"));

    let url = wait_for_stderr_value(stderr_buf.clone(), "[outrig] listen: ").await;
    assert!(
        url.starts_with("http://127.0.0.1:") && url.ends_with("/mcp"),
        "unexpected listen URL: {url}"
    );
    wait_for_stderr_value(stderr_buf.clone(), "[outrig] mcp server ready").await;

    let client = reqwest::Client::new();
    let session_a = initialize_http_session(&client, &url, 1).await;
    let session_b = initialize_http_session(&client, &url, 2).await;
    assert_ne!(
        session_a, session_b,
        "each HTTP client should get a distinct MCP session"
    );

    for (idx, session_id) in [session_a.as_str(), session_b.as_str()]
        .into_iter()
        .enumerate()
    {
        let list = post_http_mcp(
            &client,
            &url,
            session_id,
            10 + idx as u64,
            "tools/list",
            serde_json::json!({}),
        )
        .await;
        let tool_names = list["result"]["tools"]
            .as_array()
            .expect("tools array")
            .iter()
            .filter_map(|tool| tool["name"].as_str())
            .collect::<Vec<_>>();
        assert!(
            tool_names.contains(&"fs__list_directory"),
            "tools/list should include fs__list_directory: {list}"
        );

        let call = post_http_mcp(
            &client,
            &url,
            session_id,
            20 + idx as u64,
            "tools/call",
            serde_json::json!({
                "name": "fs__list_directory",
                "arguments": { "path": "/workspace" }
            }),
        )
        .await;
        assert!(
            call["result"]["isError"] != Value::Bool(true),
            "fs__list_directory should not error: {call}"
        );
        assert!(
            call.to_string().contains("HELLO.txt"),
            "list_directory should see HELLO.txt: {call}"
        );
    }

    let pid = child.id().expect("child pid").to_string();
    let term = Command::new("kill")
        .args(["-TERM", &pid])
        .status()
        .await
        .expect("send SIGTERM");
    assert!(term.success(), "kill -TERM failed with {term}");

    let status = timeout(TEST_TIMEOUT, child.wait())
        .await
        .unwrap_or_else(|_| panic!("subprocess did not exit within {TEST_TIMEOUT:?}"))
        .expect("child.wait");
    let _ = stderr_task.await;
    let stderr_str = stderr_buf.lock().unwrap().clone();
    eprintln!("--- subprocess stderr ---\n{stderr_str}");

    assert!(
        status.success(),
        "outrig mcp --listen exited with {status:?}; stderr was: {stderr_str}"
    );
    assert!(
        stderr_str.contains("[outrig] transport: streamable-http"),
        "stderr lacked streamable transport banner: {stderr_str}"
    );
    assert!(
        !stderr_str.contains("outstanding refs"),
        "HTTP shutdown should release MCP client refs before teardown: {stderr_str}"
    );

    let session_line = stderr_str
        .lines()
        .find(|l| l.contains("[outrig] container started:"))
        .expect("banner must include `container started` line");
    let our_container = session_line
        .split("started:")
        .nth(1)
        .expect("container name after `started:`")
        .trim();
    let ps = Command::new("podman")
        .args(["ps", "-a", "--filter"])
        .arg(format!("name={our_container}"))
        .args(["--format", "{{.Names}}"])
        .output()
        .await
        .expect("podman ps");
    let leftovers = String::from_utf8_lossy(&ps.stdout);
    assert!(
        leftovers.trim().is_empty(),
        "this run's container `{our_container}` is still alive: {leftovers}"
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn mcp_attach_by_session_id_reuses_container_and_writes_own_logs() {
    let _ = tracing_subscriber::fmt()
        .with_max_level(tracing::Level::INFO)
        .with_writer(std::io::stderr)
        .with_ansi(false)
        .try_init();

    let repo_dir = tempfile::tempdir().expect("tempdir repo");
    write_mcp_config(repo_dir.path());
    std::fs::write(repo_dir.path().join("HELLO.txt"), "hi\n").expect("write HELLO.txt");

    let image = ensure_fixture_image().await;
    let container = start_fixture_container(&image, repo_dir.path()).await;
    let sessions = tempfile::tempdir().expect("tempdir sessions");
    let host_sid = create_host_session(sessions.path(), repo_dir.path(), &container, &image);

    let host_log_dir = sessions.path().join(host_sid.as_str()).join("logs");
    let host_client = McpClient::connect_via_podman_exec(
        &container,
        &fs_spec(),
        "fs",
        &host_log_dir,
        &BTreeMap::new(),
    )
    .await
    .expect("host MCP client");

    let args = vec![
        "--session-root".to_string(),
        sessions.path().display().to_string(),
        "mcp".to_string(),
        "--attach".to_string(),
        host_sid.to_string(),
    ];
    let run = run_mcp_client(&args, repo_dir.path()).await;
    assert!(
        run.status.success(),
        "outrig mcp --attach exited with {:?}; stderr was: {}",
        run.status,
        run.stderr
    );
    assert!(
        run.stderr.contains("[outrig] container attached:"),
        "stderr lacked attached banner: {}",
        run.stderr
    );

    let attached_sid = stderr_value(&run.stderr, "[outrig] session id:");
    assert_ne!(
        attached_sid,
        host_sid.as_str(),
        "attacher should write a fresh session row"
    );
    let attached_log = sessions
        .path()
        .join(attached_sid)
        .join("logs")
        .join("fs.stderr");
    assert!(
        attached_log.exists(),
        "attached MCP stderr should land at {}",
        attached_log.display()
    );

    let host_result = host_client
        .call_tool("list_directory", serde_json::json!({"path": "/workspace"}))
        .await
        .expect("host MCP still answers after attach exits");
    assert!(
        !host_result.is_error && host_result.content_text.contains("HELLO.txt"),
        "host MCP child should remain usable: {host_result:?}"
    );

    let bin = env!("CARGO_BIN_EXE_outrig");
    let logs = Command::new(bin)
        .args([
            "--session-root",
            sessions.path().to_str().expect("session root utf-8"),
            "logs",
            attached_sid,
            "fs",
        ])
        .output()
        .await
        .expect("outrig logs attached session");
    assert!(
        logs.status.success(),
        "`outrig logs` for attached session failed: stderr={}",
        String::from_utf8_lossy(&logs.stderr)
    );

    host_client.shutdown().await.expect("shutdown host mcp");
    assert!(
        Container::is_running(container.name())
            .await
            .expect("podman inspect"),
        "attacher shutdown must leave host container running"
    );
    container.stop(Duration::from_secs(2)).await.expect("stop");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn mcp_attach_by_podman_name_requires_container_config_and_borrows_lifecycle() {
    let _ = tracing_subscriber::fmt()
        .with_max_level(tracing::Level::INFO)
        .with_writer(std::io::stderr)
        .with_ansi(false)
        .try_init();

    let repo_dir = tempfile::tempdir().expect("tempdir repo");
    write_mcp_config(repo_dir.path());
    std::fs::write(repo_dir.path().join("HELLO.txt"), "hi\n").expect("write HELLO.txt");

    let image = ensure_fixture_image().await;
    let container = start_fixture_container(&image, repo_dir.path()).await;
    let sessions = tempfile::tempdir().expect("tempdir sessions");

    let args = vec![
        "--session-root".to_string(),
        sessions.path().display().to_string(),
        "mcp".to_string(),
        "--attach".to_string(),
        container.name().to_string(),
        "--image".to_string(),
        "smoke".to_string(),
    ];
    let run = run_mcp_client(&args, repo_dir.path()).await;
    assert!(
        run.status.success(),
        "direct attach exited with {:?}; stderr was: {}",
        run.status,
        run.stderr
    );
    assert!(
        Container::is_running(container.name())
            .await
            .expect("podman inspect"),
        "direct attacher must not stop the borrowed container"
    );

    container.stop(Duration::from_secs(2)).await.expect("stop");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn mcp_attach_exits_when_host_stops_container() {
    let _ = tracing_subscriber::fmt()
        .with_max_level(tracing::Level::INFO)
        .with_writer(std::io::stderr)
        .with_ansi(false)
        .try_init();

    let repo_dir = tempfile::tempdir().expect("tempdir repo");
    write_mcp_config(repo_dir.path());
    std::fs::write(repo_dir.path().join("HELLO.txt"), "hi\n").expect("write HELLO.txt");

    let image = ensure_fixture_image().await;
    let container = start_fixture_container(&image, repo_dir.path()).await;
    let sessions = tempfile::tempdir().expect("tempdir sessions");
    let host_sid = create_host_session(sessions.path(), repo_dir.path(), &container, &image);

    let bin = env!("CARGO_BIN_EXE_outrig");
    let mut child = Command::new(bin)
        .args([
            "--session-root",
            sessions.path().to_str().expect("session root utf-8"),
            "mcp",
            "--attach",
            host_sid.as_str(),
        ])
        .current_dir(repo_dir.path())
        .env("OUTRIG_LOG", "info")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .kill_on_drop(true)
        .spawn()
        .expect("spawn outrig mcp --attach");

    let child_stdin = child.stdin.take().expect("stdin piped");
    let child_stdout = child.stdout.take().expect("stdout piped");
    let stderr = child.stderr.take().expect("stderr piped");
    let stderr_buf = Arc::new(Mutex::new(String::new()));
    let stderr_task = tokio::spawn(stream_lines(stderr, stderr_buf.clone(), "stderr"));

    let service = serve_client((), (child_stdout, child_stdin))
        .await
        .expect("serve_client (initialize handshake)");
    service
        .list_tools(Default::default())
        .await
        .expect("tools/list before host stop");

    container
        .stop(Duration::from_secs(2))
        .await
        .expect("host stop");

    let status = timeout(TEST_TIMEOUT, child.wait())
        .await
        .unwrap_or_else(|_| panic!("subprocess did not exit within {TEST_TIMEOUT:?}"))
        .expect("child.wait");
    drop(service);
    let _ = stderr_task.await;
    let stderr = stderr_buf.lock().unwrap().clone();
    eprintln!("--- subprocess stderr ---\n{stderr}");

    assert!(
        !status.success(),
        "attacher should exit non-zero when host stops container; stderr was: {stderr}"
    );
    assert!(
        stderr.contains("attached container") && stderr.contains("stopped"),
        "stderr should explain host container stop: {stderr}"
    );
}