croft-software 0.2.132

VSCode-style TUI written in Rust
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
//! Headless collab participant exposed as an MCP server (docs/MULTIPLAYER.md,
//! Phase D). `croft collab-agent --workspace <path>` joins the workspace's
//! collab relay as a regular guest and serves five `collab_*` tools over
//! JSON-RPC 2.0 on stdio, so an external agent (e.g. Claude Code, registered
//! with `claude mcp add`) co-edits with a live named caret. It speaks both MCP
//! eras: `server/discover` and per-request `_meta` (2026-07-28, what Claude
//! Code now opens with on stdio) and the legacy `initialize` handshake.
//!
//! Croft stays free of LLM code: the intelligence is whatever drives the
//! tools; this module is just a seat at the table, and it inherits every
//! guest property for free — owner-only disk writes, per-file site ids,
//! convergence via the CRDT.
//!
//! Concurrency: one background thread pumps the session (drains the socket,
//! integrates remote ops, tracks peer carets) so the replica is always fresh
//! when a tool call diffs against it; the main thread blocks on stdin. Both
//! share the state behind a mutex that is never held across a sleep.

use std::collections::HashMap;
use std::io::{Read, Write};
use std::path::Path;
use std::sync::Mutex;
use std::time::{Duration, Instant};

use anyhow::{Context, Result};
use serde_json::{Value, json};

use crate::collab::{CollabChannel, CollabEvent, CollabRole, CollabSession};
use crate::mcp::client as mcp;

/// How long `collab_open` waits for the owner's snapshot before reporting
/// that nobody serves this workspace: a hair past the session's own 3s
/// bootstrap deadline, so the session's verdict always lands first.
const OPEN_TIMEOUT: Duration = Duration::from_secs(4);

/// The guest session plus peers' last-seen carets (for `collab_status`).
struct AgentState {
    session: CollabSession,
    peers: HashMap<u64, PeerCaret>,
    /// The workspace the seat was started for, named in errors that tell
    /// the user how to serve it.
    workspace: std::path::PathBuf,
}

struct PeerCaret {
    name: String,
    file: String,
    row: usize,
    col: usize,
}

/// Connect to the relay as a guest, retrying briefly (the dispatch just
/// ensured the relay, but its accept loop may still be coming up).
fn connect_state(socket: &Path, workspace: &Path, name: &str) -> Result<AgentState> {
    let deadline = Instant::now() + Duration::from_secs(5);
    let channel = loop {
        if let Some(ch) = CollabChannel::connect(socket, CollabRole::Guest) {
            break ch;
        }
        anyhow::ensure!(
            Instant::now() < deadline,
            "collab relay never came up at {}",
            socket.display()
        );
        std::thread::sleep(Duration::from_millis(10));
    };
    Ok(AgentState {
        session: CollabSession::new(channel, name.to_string()),
        peers: HashMap::new(),
        workspace: workspace.to_path_buf(),
    })
}

/// One session pump: drain the socket and fold peer carets into the map.
/// Remote edits need nothing further — the replica inside the session is
/// already updated, and the agent has no editor buffers to patch.
fn pump(state: &Mutex<AgentState>) {
    let mut st = state.lock().unwrap();
    let events = st.session.poll(|_| None);
    for event in events {
        if let CollabEvent::Caret {
            file,
            site,
            row,
            col,
            name,
        } = event
        {
            st.peers.insert(
                site,
                PeerCaret {
                    name,
                    file,
                    row,
                    col,
                },
            );
        }
    }
}

/// This seat's name and version, as MCP `Implementation`.
fn server_info() -> Value {
    json!({ "name": "croft-collab", "version": env!("CARGO_PKG_VERSION") })
}

/// A JSON-RPC error: code, message, and optional `data`.
type RpcError = (i64, String, Option<Value>);

/// Check a modern request's `_meta`: the version and capabilities are
/// required, and the version must be one this seat serves statelessly.
fn check_modern_meta(meta: Option<&Value>) -> Result<(), RpcError> {
    let field = |key| meta.and_then(|m| m.get(key));
    let (Some(version), Some(_)) = (
        field(mcp::PROTOCOL_VERSION_KEY).and_then(Value::as_str),
        field(mcp::CLIENT_CAPABILITIES_KEY).filter(|caps| caps.is_object()),
    ) else {
        return Err((
            -32602,
            format!(
                "_meta needs {} as a string and {} as an object",
                mcp::PROTOCOL_VERSION_KEY,
                mcp::CLIENT_CAPABILITIES_KEY
            ),
            None,
        ));
    };
    if version != mcp::MODERN_VERSION {
        return Err((
            mcp::UNSUPPORTED_PROTOCOL_VERSION,
            String::from("Unsupported protocol version"),
            Some(json!({ "supported": mcp::SUPPORTED_VERSIONS, "requested": version })),
        ));
    }
    Ok(())
}

/// Answer one inbound JSON-RPC message; `None` for notifications (and for
/// malformed lines with no `id` to answer to).
///
/// The seat is dual-era. A request carrying a protocol version in its
/// `_meta`, and the `server/discover` probe, are modern: served statelessly,
/// and the result names its type and this server. Anything else (an
/// `initialize` handshake and the plain requests after it) is legacy and
/// answered as before.
fn handle(state: &Mutex<AgentState>, msg: &Value) -> Option<Value> {
    let method = msg
        .get("method")
        .and_then(Value::as_str)
        .unwrap_or_default();
    if matches!(
        method,
        "notifications/initialized" | "notifications/cancelled"
    ) {
        return None;
    }
    let meta = msg.pointer("/params/_meta");
    let modern = method == "server/discover"
        || meta.is_some_and(|m| m.get(mcp::PROTOCOL_VERSION_KEY).is_some());
    let checked = if modern {
        check_modern_meta(meta)
    } else {
        Ok(())
    };
    let result = checked.and_then(|()| match method {
        "server/discover" => Ok(json!({
            "supportedVersions": mcp::SUPPORTED_VERSIONS,
            "capabilities": { "tools": {} },
        })),
        "initialize" => Ok(json!({
            "protocolVersion": mcp::LEGACY_VERSION,
            "capabilities": { "tools": {} },
            "serverInfo": server_info(),
        })),
        "ping" => Ok(json!({})),
        "tools/list" => Ok(json!({ "tools": tool_definitions() })),
        "tools/call" => {
            let name = msg
                .pointer("/params/name")
                .and_then(Value::as_str)
                .unwrap_or_default()
                .to_string();
            let args = msg
                .pointer("/params/arguments")
                .cloned()
                .unwrap_or_else(|| json!({}));
            match call_tool(state, &name, &args) {
                Some(result) => Ok(result),
                None => Err((-32602, format!("unknown tool: {name}"), None)),
            }
        }
        _ => Err((-32601, format!("method not found: {method}"), None)),
    });
    let id = msg.get("id").cloned()?;
    Some(match result {
        Ok(mut result) => {
            if let (true, Some(fields)) = (modern, result.as_object_mut()) {
                fields.insert("resultType".into(), json!("complete"));
                fields.insert(
                    "_meta".into(),
                    json!({ mcp::SERVER_INFO_KEY: server_info() }),
                );
            }
            json!({ "jsonrpc": "2.0", "id": id, "result": result })
        }
        Err((code, message, data)) => {
            let mut error = json!({ "code": code, "message": message });
            if let Some(data) = data {
                error["data"] = data;
            }
            json!({ "jsonrpc": "2.0", "id": id, "error": error })
        }
    })
}

/// A successful `tools/call` result: one text content block.
fn text_result(text: impl Into<String>) -> Value {
    json!({ "content": [{ "type": "text", "text": text.into() }] })
}

/// A failed tool execution: per MCP, an `isError` result (the model reads
/// and recovers from it), not a JSON-RPC protocol error.
fn error_result(text: impl Into<String>) -> Value {
    json!({ "content": [{ "type": "text", "text": text.into() }], "isError": true })
}

/// Run one collab tool; `None` means the tool name is unknown.
fn call_tool(state: &Mutex<AgentState>, tool: &str, args: &Value) -> Option<Value> {
    let file = args
        .get("file")
        .and_then(Value::as_str)
        .unwrap_or_default()
        .to_string();
    let needs_file = tool != "collab_status";
    if needs_file && file.is_empty() {
        return Some(error_result("missing required argument: file"));
    }
    let not_live = || error_result(format!("{file} is not live; call collab_open first"));
    Some(match tool {
        "collab_open" => {
            // rejoin, not request: an earlier open that timed out latched
            // the file local-only, and a plain request respects the latch —
            // the caller retrying after starting croft would never join.
            state.lock().unwrap().session.rejoin_file(&file);
            let deadline = Instant::now() + OPEN_TIMEOUT;
            loop {
                {
                    let st = state.lock().unwrap();
                    if let Some(text) = st.session.doc_text(&file) {
                        break text_result(text);
                    }
                    if !st.session.is_bootstrapping(&file) || Instant::now() >= deadline {
                        // Only a session croft hosts serves the seat, so a
                        // plain window open on the workspace answers nobody
                        // (#1289): say what does, not "start croft".
                        let ws = st.workspace.display();
                        break error_result(format!(
                            "no croft session is sharing {ws}, so nobody served {file}: \
                             open the workspace with `croft attach {ws}` (a plain `croft` \
                             window does not host agents), then retry"
                        ));
                    }
                }
                std::thread::sleep(Duration::from_millis(20));
            }
        }
        "collab_read" => match state.lock().unwrap().session.doc_text(&file) {
            Some(text) => text_result(text),
            None => not_live(),
        },
        "collab_replace" => {
            let Some(text) = args.get("text").and_then(Value::as_str) else {
                return Some(error_result("missing required argument: text"));
            };
            let mut st = state.lock().unwrap();
            if !st.session.is_live(&file) {
                not_live()
            } else {
                st.session.local_change(&file, text);
                text_result(st.session.doc_text(&file).unwrap_or_default())
            }
        }
        "collab_caret" => {
            let row = args.get("row").and_then(Value::as_u64);
            let col = args.get("col").and_then(Value::as_u64);
            let (Some(row), Some(col)) = (row, col) else {
                return Some(error_result("row and col are required integers (0-based)"));
            };
            let mut st = state.lock().unwrap();
            if !st.session.is_live(&file) {
                not_live()
            } else {
                st.session.send_caret(&file, row as usize, col as usize);
                text_result(format!("caret at {row}:{col} in {file}"))
            }
        }
        "collab_status" => {
            let st = state.lock().unwrap();
            let files: serde_json::Map<String, Value> = st
                .session
                .tracked_files()
                .into_iter()
                .map(|(f, live)| (f, Value::from(if live { "live" } else { "bootstrapping" })))
                .collect();
            let peers: Vec<Value> = st
                .peers
                .iter()
                .map(|(site, p)| {
                    json!({
                        "site": site,
                        "name": p.name,
                        "file": p.file,
                        "row": p.row,
                        "col": p.col,
                    })
                })
                .collect();
            text_result(json!({ "files": files, "peers": peers }).to_string())
        }
        _ => return None,
    })
}

/// The five tools this server advertises. Coordinates are 0-based character
/// positions, matching the collab wire protocol.
fn tool_definitions() -> Value {
    let file_prop = json!({
        "type": "string",
        "description": "Workspace-relative path, e.g. src/main.rs",
    });
    json!([
        {
            "name": "collab_open",
            "description": "Join a workspace file in the live croft session and \
                return its current text. Call this once per file before reading \
                or editing it.",
            "inputSchema": {
                "type": "object",
                "properties": { "file": file_prop },
                "required": ["file"],
            },
        },
        {
            "name": "collab_read",
            "description": "The file's current text, including edits other \
                participants just made.",
            "inputSchema": {
                "type": "object",
                "properties": { "file": file_prop },
                "required": ["file"],
            },
        },
        {
            "name": "collab_replace",
            "description": "Replace the file's entire text; the diff is broadcast \
                to every participant as live edits (the session owner persists them \
                to disk). Returns the post-apply text. Read or open first so the \
                replacement builds on the latest text.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "file": file_prop,
                    "text": { "type": "string", "description": "The complete new file text" },
                },
                "required": ["file", "text"],
            },
        },
        {
            "name": "collab_caret",
            "description": "Move this agent's visible caret so humans see where it \
                is working (a named colored caret in their editors). 0-based row \
                and character column. Move it near each edit you make.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "file": file_prop,
                    "row": { "type": "integer", "description": "0-based line" },
                    "col": { "type": "integer", "description": "0-based character column" },
                },
                "required": ["file", "row", "col"],
            },
        },
        {
            "name": "collab_status",
            "description": "Session overview: every tracked file with its state \
                (live or bootstrapping) and each peer's last-seen caret.",
            "inputSchema": { "type": "object", "properties": {} },
        },
    ])
}

/// Serve the MCP loop on stdio until the driving client hangs up (EOF).
pub fn run(socket: &Path, workspace: &Path, name: String) -> Result<()> {
    let state = std::sync::Arc::new(Mutex::new(connect_state(socket, workspace, &name)?));
    {
        let state = std::sync::Arc::clone(&state);
        std::thread::spawn(move || {
            loop {
                pump(&state);
                std::thread::sleep(Duration::from_millis(30));
            }
        });
    }
    let mut decoder = crate::mcp::transport::LineDecoder::new();
    let mut stdin = std::io::stdin().lock();
    let stdout = std::io::stdout();
    let mut buf = [0u8; 8192];
    loop {
        let n = stdin.read(&mut buf).context("reading MCP stdin")?;
        if n == 0 {
            return Ok(()); // the driver hung up; this seat leaves the session
        }
        decoder.feed(&buf[..n]);
        while let Some(msg) = decoder.next_message() {
            if let Some(resp) = handle(&state, &msg) {
                let mut bytes = serde_json::to_vec(&resp).unwrap_or_default();
                bytes.push(b'\n');
                let mut out = stdout.lock();
                let _ = out.write_all(&bytes);
                let _ = out.flush();
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::collab::{CollabChannel, CollabEvent, CollabRole, CollabSession, relay_serve};
    use serde_json::{Value, json};
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::{Arc, Mutex};
    use std::time::{Duration, Instant};

    fn rpc(id: i64, method: &str, params: Value) -> Value {
        json!({ "jsonrpc": "2.0", "id": id, "method": method, "params": params })
    }

    fn call(state: &Mutex<AgentState>, id: i64, tool: &str, args: Value) -> Value {
        handle(
            state,
            &rpc(id, "tools/call", json!({ "name": tool, "arguments": args })),
        )
        .expect("tools/call always answers")
    }

    fn text_of(resp: &Value) -> String {
        crate::mcp::client::tool_result_text(&resp["result"])
    }

    /// A live relay plus a connected agent state, ready to drive `handle`.
    fn agent_over_relay() -> (tempfile::TempDir, std::path::PathBuf, Mutex<AgentState>) {
        let dir = tempfile::tempdir().unwrap();
        let socket = dir.path().join("a.collab.sock");
        {
            let s = socket.clone();
            std::thread::spawn(move || {
                let _ = relay_serve(&s);
            });
        }
        let state = connect_state(&socket, std::path::Path::new("/work/space"), "claude")
            .expect("agent connects");
        (dir, socket, Mutex::new(state))
    }

    /// The MCP surface: initialize/tools-list shapes mirror what croft's own
    /// client sends, unknown methods get JSON-RPC errors, and tools on files
    /// that are not live fail as tool errors (isError), not protocol errors.
    #[test]
    fn collab_agent_answers_the_mcp_surface() {
        let (_dir, _socket, state) = agent_over_relay();

        let init = handle(&state, &rpc(1, "initialize", json!({}))).unwrap();
        assert_eq!(init["result"]["protocolVersion"], mcp::LEGACY_VERSION);
        assert_eq!(init["result"]["serverInfo"]["name"], "croft-collab");
        assert!(
            init["result"].get("resultType").is_none(),
            "a legacy answer stays legacy"
        );
        assert!(
            handle(
                &state,
                &json!({ "jsonrpc": "2.0", "method": "notifications/initialized" })
            )
            .is_none(),
            "notifications draw no response"
        );

        let tools = handle(&state, &rpc(2, "tools/list", json!({}))).unwrap();
        let names: Vec<&str> = tools["result"]["tools"]
            .as_array()
            .unwrap()
            .iter()
            .map(|t| t["name"].as_str().unwrap())
            .collect();
        assert_eq!(
            names,
            [
                "collab_open",
                "collab_read",
                "collab_replace",
                "collab_caret",
                "collab_status"
            ]
        );

        let unknown = handle(&state, &rpc(3, "bogus/method", json!({}))).unwrap();
        assert_eq!(unknown["error"]["code"], -32601);

        let read = call(&state, 4, "collab_read", json!({ "file": "src/f.rs" }));
        assert_eq!(read["result"]["isError"], true);
        assert!(
            text_of(&read).contains("collab_open"),
            "the error teaches the fix: {}",
            text_of(&read)
        );
    }

    /// A modern request: `params` with croft's own client `_meta` at `version`.
    fn modern(id: i64, method: &str, params: Value, version: &str) -> Value {
        let mut meta = mcp::request_meta("claude-code", "test");
        meta.insert(mcp::PROTOCOL_VERSION_KEY.into(), json!(version));
        rpc(id, method, mcp::with_meta(params, &meta))
    }

    /// The 2026-07-28 surface, as Claude Code now opens it on stdio: the
    /// discover probe answers with both eras' versions (and croft's own
    /// client reads it as modern), and a request carrying `_meta` is served
    /// with no `initialize`, its result typed and naming this server.
    #[test]
    fn collab_agent_answers_a_modern_client_without_a_handshake() {
        let (_dir, _socket, state) = agent_over_relay();

        let probe = modern(1, "server/discover", json!({}), mcp::MODERN_VERSION);
        let discovered = handle(&state, &probe).unwrap();
        let result = &discovered["result"];
        assert_eq!(result["supportedVersions"], json!(mcp::SUPPORTED_VERSIONS));
        assert_eq!(result["capabilities"]["tools"], json!({}));
        assert_eq!(result["resultType"], "complete");
        assert_eq!(
            result["_meta"][mcp::SERVER_INFO_KEY]["name"],
            "croft-collab"
        );
        assert_eq!(mcp::classify_probe(&discovered).unwrap(), mcp::Era::Modern);

        let list = modern(2, "tools/list", json!({}), mcp::MODERN_VERSION);
        let tools = handle(&state, &list).unwrap();
        assert_eq!(tools["result"]["tools"].as_array().unwrap().len(), 5);
        assert_eq!(tools["result"]["resultType"], "complete");

        let read = modern(
            3,
            "tools/call",
            json!({ "name": "collab_read", "arguments": { "file": "src/f.rs" } }),
            mcp::MODERN_VERSION,
        );
        let read = handle(&state, &read).unwrap();
        assert_eq!(read["result"]["isError"], true);
        assert_eq!(read["result"]["resultType"], "complete");
    }

    /// A modern request at a version the seat does not serve gets the
    /// versions it does, and one missing the required `_meta` fields is
    /// invalid params rather than a silent legacy answer.
    #[test]
    fn collab_agent_rejects_unservable_modern_requests() {
        let (_dir, _socket, state) = agent_over_relay();

        let probe = modern(1, "server/discover", json!({}), "1900-01-01");
        let unsupported = handle(&state, &probe).unwrap();
        assert_eq!(
            unsupported["error"]["code"],
            mcp::UNSUPPORTED_PROTOCOL_VERSION
        );
        assert_eq!(
            unsupported["error"]["data"]["supported"],
            json!(mcp::SUPPORTED_VERSIONS)
        );
        assert_eq!(unsupported["error"]["data"]["requested"], "1900-01-01");
        // A dual-era client retries at a version from that list.
        assert_eq!(mcp::classify_probe(&unsupported).unwrap(), mcp::Era::Modern);

        let bare = json!({ "_meta": { mcp::PROTOCOL_VERSION_KEY: mcp::MODERN_VERSION } });
        let invalid = handle(&state, &rpc(2, "tools/list", bare)).unwrap();
        assert_eq!(invalid["error"]["code"], -32602);
        let not_an_object = json!({ "_meta": {
            mcp::PROTOCOL_VERSION_KEY: mcp::MODERN_VERSION,
            mcp::CLIENT_CAPABILITIES_KEY: null,
        } });
        let invalid = handle(&state, &rpc(3, "tools/list", not_an_object)).unwrap();
        assert_eq!(invalid["error"]["code"], -32602);
    }

    /// No owner on the relay: `collab_open` fails with what the user has to
    /// do, not "start croft" (#1289). A plain `croft` window is running in
    /// the workspace in that case and serves nobody, so the error names
    /// `croft attach` and the workspace it was given.
    #[test]
    fn opening_with_no_owner_says_the_workspace_needs_croft_attach() {
        let (_dir, _socket, state) = agent_over_relay();
        let opened = call(&state, 1, "collab_open", json!({ "file": "src/m.rs" }));
        assert_eq!(opened["result"]["isError"], true);
        let msg = text_of(&opened);
        assert!(
            msg.contains("croft attach /work/space"),
            "names the command and the workspace: {msg}"
        );
        assert!(
            msg.contains("a plain `croft` window does not host agents"),
            "says why the open window is not enough: {msg}"
        );
        assert!(!msg.contains("start croft in this workspace"), "{msg}");
    }

    /// End to end over a real relay: the agent opens a file served by a live
    /// owner, replaces its text, and moves its caret; the owner's replica
    /// converges and its events carry the caret under the agent's name.
    #[test]
    fn collab_agent_opens_edits_and_carets_through_the_relay() {
        let (_dir, socket, state) = agent_over_relay();
        let state = Arc::new(state);

        let deadline = Instant::now() + Duration::from_secs(5);
        let owner = loop {
            if let Some(ch) = CollabChannel::connect(&socket, CollabRole::Owner) {
                break CollabSession::new(ch, "owner".into());
            }
            assert!(Instant::now() < deadline, "relay never came up");
            std::thread::sleep(Duration::from_millis(10));
        };
        let owner = Arc::new(Mutex::new(owner));
        let owner_events: Arc<Mutex<Vec<CollabEvent>>> = Arc::new(Mutex::new(Vec::new()));
        let stop = Arc::new(AtomicBool::new(false));

        // Owner pump: serves one file's text and collects events.
        let owner_pump = {
            let (owner, events, stop) = (owner.clone(), owner_events.clone(), stop.clone());
            std::thread::spawn(move || {
                while !stop.load(Ordering::Relaxed) {
                    {
                        let mut o = owner.lock().unwrap();
                        let ev = o.poll(|_| Some("hello world".to_string()));
                        events.lock().unwrap().extend(ev);
                    }
                    std::thread::sleep(Duration::from_millis(5));
                }
            })
        };
        // Agent pump, exactly as `run` spawns it.
        let agent_pump = {
            let (state, stop) = (state.clone(), stop.clone());
            std::thread::spawn(move || {
                while !stop.load(Ordering::Relaxed) {
                    pump(&state);
                    std::thread::sleep(Duration::from_millis(5));
                }
            })
        };

        let opened = call(&state, 1, "collab_open", json!({ "file": "src/f.rs" }));
        assert_eq!(text_of(&opened), "hello world");

        let replaced = call(
            &state,
            2,
            "collab_replace",
            json!({ "file": "src/f.rs", "text": "hello brave world" }),
        );
        assert_eq!(text_of(&replaced), "hello brave world");
        let deadline = Instant::now() + Duration::from_secs(5);
        while owner.lock().unwrap().doc_text("src/f.rs") != Some("hello brave world") {
            assert!(Instant::now() < deadline, "owner never converged");
            std::thread::sleep(Duration::from_millis(5));
        }

        call(
            &state,
            3,
            "collab_caret",
            json!({ "file": "src/f.rs", "row": 0, "col": 6 }),
        );
        let deadline = Instant::now() + Duration::from_secs(5);
        loop {
            let seen = owner_events.lock().unwrap().iter().any(|e| {
                matches!(e, CollabEvent::Caret { name, row: 0, col: 6, .. } if name == "claude")
            });
            if seen {
                break;
            }
            assert!(Instant::now() < deadline, "owner never saw the agent caret");
            std::thread::sleep(Duration::from_millis(5));
        }

        let status = call(&state, 4, "collab_status", json!({}));
        let status_text = text_of(&status);
        assert!(status_text.contains("src/f.rs"), "status lists the file");
        assert!(status_text.contains("live"), "status shows the live state");

        stop.store(true, Ordering::Relaxed);
        let _ = (owner_pump.join(), agent_pump.join());
    }
}