myco 0.3.0

Multi-host coding agent CLI (local in-process + SSH remotes)
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
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
//! Agent-side harness: always-on in-process local host + optional remotes.
//!
//! - **local**: tools run in-process via [`HostController::in_process`] (no subprocess).
//! - **remotes**: lazy `ssh … myco --mode host` over NDJSON.
//! - **Root-only services**: extra [`ToolService`]s (e.g. `session_meta`, `subagent`)
//!   are installed only on the local worker at attach time — still host tools,
//!   configured on root.

use std::collections::HashMap;
use std::sync::Arc;

use crate::core::{Async, CancelToken};
use crate::generative_model;
use crate::session::{EventSink, TraceContext};
use crate::tool_services::ToolService;

mod config;
pub use config::{
    AuthEntry, FileConfig, GatewayEntry, ModelEntry, default_ssh_config_path, example_config_toml,
    load_file_config, load_ssh_host_aliases, parse_file_config_str, ssh_config_host_aliases,
    ssh_spawn_command,
};

// HostController lives in `crate::host` (in-process local or remote subprocess).
pub use crate::host::{HostConfig, HostController};

mod subagent_service;
pub use subagent_service::{AgentRootHandles, SubagentService};

mod ssh;
pub use ssh::{
    SshAgentPreflightReport, ensure_remote_ssh_identities, print_preflight_report,
    ssh_destination_from_command,
};

/// Snapshot of one configured host.
#[derive(Debug, Clone)]
pub struct HostStatus {
    pub name: String,
    /// Display command (`in-process` for local, ssh argv for remotes).
    pub command: Vec<String>,
    /// Live worker connection is currently open (always true for in-process local).
    pub connected: bool,
    /// True when tools run inside the agent process (local).
    pub in_process: bool,
    pub tools: Vec<String>,
    /// Last connect failure (if any); cleared after a successful connect.
    pub error: Option<String>,
}

/// Agent-facing tool runtime: routes tools to hosts.
pub struct Harness {
    /// All hosts including always-present `"local"`.
    hosts: HashMap<String, Arc<HostController>>,
    /// Full command lines for display (`in-process` stored as a single-token vec).
    host_commands: HashMap<String, Vec<String>>,
    /// Always `"local"` — tools that omit `host` run here.
    default_host: String,
    /// Union of tool names known across hosts (standard + root-only extras on local).
    host_tool_names: std::collections::HashSet<String>,
    /// Tools installed only on the in-process local worker (not remotes).
    ///
    /// These always route to `local` and **must not** receive the injected routing
    /// `host` field — their own schemas may use `host` for other purposes
    /// (e.g. `session_meta` worktree links).
    root_only_tool_names: std::collections::HashSet<String>,
    /// Cached tool specs advertised to the model (host field injected for multi-host tools).
    tool_specs: Vec<generative_model::ToolSpec>,
    /// Local host's text-search engine. Attach never indexes; the owning
    /// process opts in via [`Self::auto_index_local`].
    local_search: crate::text_search::TextSearchEngine,
}

/// How to construct a harness.
#[derive(Debug, Clone)]
pub struct HarnessConfig {
    /// Remote hosts only. Local is always added in-process by [`Harness::attach`].
    pub remote_hosts: Vec<HostConfig>,
    /// When true (default), register [`SubagentService`] as a local tool.
    pub enable_subagent: bool,
    /// Per-remote connect timeout in seconds on first tool use (`0` disables it).
    pub attach_timeout_secs: u64,
    /// Resolved model catalog for subagents (`crate::config`). Empty (default)
    /// means the `subagent` tool rejects every model key.
    pub models: crate::generative_model::ModelCatalog,
}

impl Default for HarnessConfig {
    fn default() -> Self {
        Self {
            remote_hosts: Vec::new(),
            enable_subagent: true,
            attach_timeout_secs: 10,
            models: crate::generative_model::ModelCatalog::default(),
        }
    }
}

/// Resolve `myco --mode host` argv (used by tests that still spawn a local subprocess).
pub fn default_local_host_command() -> Vec<String> {
    vec![
        myco_program(),
        "--mode".into(),
        "host".into(),
        "--name".into(),
        "local".into(),
    ]
}

pub(crate) fn myco_program() -> String {
    // cargo integration tests set this when the package builds the binary.
    if let Ok(path) = std::env::var("CARGO_BIN_EXE_myco")
        && std::path::Path::new(&path).is_file()
    {
        return path;
    }
    // Sibling of current exe (installed layout / `cargo run` target dir).
    if let Ok(exe) = std::env::current_exe()
        && let Some(dir) = exe.parent()
    {
        for name in ["myco", "myco.exe"] {
            let candidate = dir.join(name);
            if candidate.is_file() {
                return candidate.to_string_lossy().into_owned();
            }
        }
        // Unit tests run as target/debug/deps/… — walk up to target/{debug,release}/.
        if let Some(parent) = dir.parent() {
            for name in ["myco", "myco.exe"] {
                let candidate = parent.join(name);
                if candidate.is_file() {
                    return candidate.to_string_lossy().into_owned();
                }
            }
        }
    }
    // Dev fallback via CARGO_MANIFEST_DIR.
    if let Ok(manifest) = std::env::var("CARGO_MANIFEST_DIR") {
        for profile in ["debug", "release"] {
            let candidate = std::path::Path::new(&manifest)
                .join("target")
                .join(profile)
                .join("myco");
            if candidate.is_file() {
                return candidate.to_string_lossy().into_owned();
            }
        }
    }
    "myco".into()
}

impl Harness {
    /// Register the always-on in-process local host, optional remotes, and local tools.
    ///
    /// Remotes start with `conn = None` and connect on first tool call (bounded by
    /// `attach_timeout_secs`). Local is ready immediately — no subprocess.
    /// Register the always-on in-process local host and optional remotes.
    ///
    /// When `enable_subagent` is true, installs [`SubagentService`] on the **local**
    /// worker only. Use [`Self::attach_with_root_services`] to add more root-only services
    /// (e.g. `session_meta`).
    pub async fn attach(config: HarnessConfig) -> Result<Arc<Self>, String> {
        Self::attach_with_root_services(config, Vec::new()).await
    }

    /// Like [`Self::attach`], plus extra [`ToolService`]s installed **only** on the
    /// in-process `local` worker (configuration-layer local-only tools).
    pub async fn attach_with_root_services(
        config: HarnessConfig,
        root_services: Vec<Arc<dyn ToolService>>,
    ) -> Result<Arc<Self>, String> {
        for h in &config.remote_hosts {
            if h.name == "local" {
                return Err(
                    "remote host name \"local\" is reserved; local is always in-process".into(),
                );
            }
            if h.name.trim().is_empty() {
                return Err("remote host with empty name".into());
            }
        }

        let mut hosts = HashMap::new();
        let mut host_commands = HashMap::new();
        let mut host_tool_names = std::collections::HashSet::new();
        let mut root_only_tool_names = std::collections::HashSet::new();
        let mut tool_specs = Vec::new();
        let mut seen_tools = HashMap::<String, ()>::new();

        // Standard catalog (every host, including remotes) — inject routing `host`.
        for spec in crate::host::HostWorker::standard_tool_specs() {
            host_tool_names.insert(spec.name.clone());
            if seen_tools.insert(spec.name.clone(), ()).is_none() {
                tool_specs.push(inject_host_field(spec));
            }
        }

        // Root-only extras (subagent, session_meta, …) — local only; keep their schemas.
        let mut root_extras: Vec<Arc<dyn ToolService>> = Vec::new();
        if config.enable_subagent {
            root_extras
                .push(Arc::new(SubagentService::new(config.models.clone())) as Arc<dyn ToolService>);
        }
        root_extras.extend(root_services);

        for service in &root_extras {
            for spec in service.tool_specs() {
                host_tool_names.insert(spec.name.clone());
                root_only_tool_names.insert(spec.name.clone());
                if seen_tools.insert(spec.name.clone(), ()).is_none() {
                    // Do **not** inject routing `host` — root tools may use `host` themselves.
                    tool_specs.push(spec);
                }
            }
        }

        // Keep the local engine handle on the harness so the owning process
        // can request auto-indexing after attach; attach itself never indexes.
        let local_search = crate::text_search::TextSearchEngine::new();
        let mut local_services: Vec<Arc<dyn ToolService>> =
            crate::host::HostWorker::services_with_search(
                crate::tool_services::TextSearchToolService::with_engine(local_search.clone()),
            );
        local_services.extend(root_extras);

        let local_worker = Arc::new(crate::host::HostWorker::new("local", local_services));
        let local = HostController::in_process("local", local_worker);
        host_commands.insert("local".into(), vec!["in-process".into()]);
        hosts.insert("local".into(), local);

        let connect_timeout = config.attach_timeout_secs;
        for host_cfg in config.remote_hosts {
            host_commands.insert(host_cfg.name.clone(), host_cfg.command.clone());
            let name = host_cfg.name.clone();
            hosts.insert(
                name,
                HostController::with_timeout(host_cfg, connect_timeout),
            );
        }

        Ok(Arc::new(Self {
            hosts,
            host_commands,
            default_host: "local".into(),
            host_tool_names,
            root_only_tool_names,
            tool_specs,
            local_search,
        }))
    }

    /// Owner opt-in: discover and index skills / AGENTS.md under `cwd` on the
    /// in-process local host (background crawl on the blocking pool).
    ///
    /// Called by the interactive CLI entrypoint. Tests that merely attach a
    /// harness never index anything.
    pub fn auto_index_local(&self, cwd: std::path::PathBuf) {
        self.local_search.auto_index_under(cwd);
    }

    /// Test helper: attach only the in-process local host, no subagent.
    pub async fn attach_local_for_tests() -> Result<Arc<Self>, String> {
        Self::attach(HarnessConfig {
            enable_subagent: false,
            ..HarnessConfig::default()
        })
        .await
    }

    /// In-process harness for unit tests: local host only, with the given services
    /// (plus the standard catalog).
    pub fn local_with_services(extra: Vec<Arc<dyn ToolService>>) -> Arc<Self> {
        let local_search = crate::text_search::TextSearchEngine::new();
        let standard = crate::host::HostWorker::services_with_search(
            crate::tool_services::TextSearchToolService::with_engine(local_search.clone()),
        );
        let mut host_tool_names = std::collections::HashSet::new();
        let mut root_only_tool_names = std::collections::HashSet::new();
        let mut tool_specs = Vec::new();
        let mut seen = HashMap::<String, ()>::new();
        for service in &standard {
            for spec in service.tool_specs() {
                host_tool_names.insert(spec.name.clone());
                if seen.insert(spec.name.clone(), ()).is_none() {
                    tool_specs.push(inject_host_field(spec));
                }
            }
        }
        for service in &extra {
            for spec in service.tool_specs() {
                host_tool_names.insert(spec.name.clone());
                root_only_tool_names.insert(spec.name.clone());
                if seen.insert(spec.name.clone(), ()).is_none() {
                    tool_specs.push(spec);
                }
            }
        }
        let mut services = standard;
        services.extend(extra);
        let worker = Arc::new(crate::host::HostWorker::new("local", services));
        let local = HostController::in_process("local", worker);
        let mut hosts = HashMap::new();
        let mut host_commands = HashMap::new();
        hosts.insert("local".into(), local);
        host_commands.insert("local".into(), vec!["in-process".into()]);
        Arc::new(Self {
            hosts,
            host_commands,
            default_host: "local".into(),
            host_tool_names,
            root_only_tool_names,
            tool_specs,
            local_search,
        })
    }

    pub fn tool_specs(&self) -> Vec<generative_model::ToolSpec> {
        self.tool_specs.clone()
    }

    /// Always `"local"`.
    pub fn default_host(&self) -> &str {
        &self.default_host
    }

    pub fn host_names(&self) -> Vec<String> {
        let mut names: Vec<_> = self.hosts.keys().cloned().collect();
        names.sort();
        // Keep local first for display.
        if let Some(i) = names.iter().position(|n| n == "local") {
            let local = names.remove(i);
            names.insert(0, local);
        }
        names
    }

    /// Status table for configured hosts (local always ok/in-process; remotes idle/ok/DOWN).
    pub fn host_status(&self) -> Vec<HostStatus> {
        self.host_names()
            .into_iter()
            .map(|name| {
                let command = self.host_commands.get(&name).cloned().unwrap_or_default();
                let client = self.hosts.get(&name).expect("host map key");
                HostStatus {
                    connected: client.is_connected(),
                    in_process: client.is_in_process(),
                    tools: client.tool_specs().iter().map(|t| t.name.clone()).collect(),
                    error: client.last_error(),
                    name,
                    command,
                }
            })
            .collect()
    }

    pub fn dispatch_tool_use(
        self: Arc<Self>,
        mut tool_use: generative_model::ToolUse,
        sink: Arc<dyn EventSink>,
        context: TraceContext,
        cancel: CancelToken,
    ) -> Async<generative_model::ToolResult> {
        Box::pin(async move {
            let id = tool_use.id.clone();
            let name = tool_use.name.clone();

            if !self.host_tool_names.contains(&name) {
                return generative_model::ToolResult::err(format!("unknown tool '{name}'"))
                    .with_id(id);
            }

            // Root-only tools always run on the in-process local worker. Their schemas
            // may use `host` for non-routing purposes (e.g. session_meta worktree links),
            // so do not resolve/strip a routing host field for them.
            let host_name = if self.root_only_tool_names.contains(&name) {
                self.default_host.clone()
            } else {
                match resolve_host_for_call(&tool_use, &self.default_host) {
                    Ok(h) => h,
                    Err(e) => return generative_model::ToolResult::err(e).with_id(id),
                }
            };

            if !self.root_only_tool_names.contains(&name) {
                // Strip routing-only `host` before forwarding multi-host tools.
                strip_host_field(&mut tool_use);
            }

            let Some(client) = self.hosts.get(&host_name).cloned() else {
                let known = self.host_names().join(", ");
                return generative_model::ToolResult::err(format!(
                    "unknown host {host_name:?} (known: [{known}]; default={})",
                    self.default_host
                ))
                .with_id(id);
            };

            // Root handles for in-process local (subagent etc.); remotes ignore.
            let agent_root: Option<Arc<dyn std::any::Any + Send + Sync>> = if client.is_in_process()
            {
                Some(Arc::new(crate::harness::AgentRootHandles {
                    harness: self.clone(),
                    sink,
                    context: context.clone(),
                }) as Arc<dyn std::any::Any + Send + Sync>)
            } else {
                None
            };

            client
                .call_with_root(context.agent_id, tool_use, cancel, agent_root)
                .await
        })
    }

    /// Notify all hosts that `agent_id`'s session ended.
    ///
    /// Safe to call from [`Drop`]: schedules work on the current tokio runtime when
    /// available. Host process exit (via HostController drop) is the hard guarantee
    /// for remotes; in-process local reaps via the worker directly.
    pub fn notify_agent_finished(&self, agent_id: uuid::Uuid) {
        let clients: Vec<_> = self.hosts.values().cloned().collect();
        if clients.is_empty() {
            return;
        }
        if let Ok(handle) = tokio::runtime::Handle::try_current() {
            handle.spawn(async move {
                for c in clients {
                    if let Err(e) = c.agent_finished(agent_id).await {
                        eprintln!("warning: agent_finished on host {:?}: {e}", c.name);
                    }
                }
            });
        }
    }
}

/// Read optional `host` from tool input; default to `default_host` (always `"local"`).
fn resolve_host_for_call(
    tool_use: &generative_model::ToolUse,
    default_host: &str,
) -> Result<String, String> {
    match tool_use.input.get("host") {
        None | Some(serde_json::Value::Null) => Ok(default_host.to_string()),
        Some(serde_json::Value::String(s)) => {
            let s = s.trim();
            if s.is_empty() {
                Ok(default_host.to_string())
            } else {
                Ok(s.to_string())
            }
        }
        Some(other) => Err(format!(
            "tool input field `host` must be a string, got {other}"
        )),
    }
}

fn strip_host_field(tool_use: &mut generative_model::ToolUse) {
    if let serde_json::Value::Object(map) = &mut tool_use.input {
        map.remove("host");
    }
}

/// Inject optional `host` into a host tool's JSON schema so models can target machines.
fn inject_host_field(mut spec: generative_model::ToolSpec) -> generative_model::ToolSpec {
    let schema = &mut spec.input_schema;
    if !schema.is_object() {
        return spec;
    }
    let Some(props) = schema
        .as_object_mut()
        .and_then(|o| o.get_mut("properties"))
        .and_then(|p| p.as_object_mut())
    else {
        // Ensure properties object exists.
        if let Some(obj) = schema.as_object_mut() {
            obj.entry("properties")
                .or_insert_with(|| serde_json::json!({}));
            if let Some(props) = obj.get_mut("properties").and_then(|p| p.as_object_mut()) {
                props.insert(
                    "host".into(),
                    serde_json::json!({
                        "type": ["string", "null"],
                        "description":
                            "Target host name (optional; defaults to \"local\"). Local is always in-process; remotes are named in config. Sessions are per-host.",
                    }),
                );
            }
        }
        return spec;
    };

    if !props.contains_key("host") {
        props.insert(
            "host".into(),
            serde_json::json!({
                "type": ["string", "null"],
                "description":
                    "Target host name (optional; defaults to \"local\"). Local is always in-process; remotes are named in config. Sessions are per-host.",
            }),
        );
    }
    // Do not add `host` to required[].
    spec
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::generative_model::ToolUse;
    use serde_json::json;

    #[test]
    fn resolve_host_defaults_and_overrides() {
        let tu = ToolUse {
            id: "1".into(),
            name: "bash".into(),
            input: json!({"command": "echo"}),
        };
        assert_eq!(resolve_host_for_call(&tu, "local").unwrap(), "local");

        let tu = ToolUse {
            id: "1".into(),
            name: "bash".into(),
            input: json!({"command": "echo", "host": "devbox"}),
        };
        assert_eq!(resolve_host_for_call(&tu, "local").unwrap(), "devbox");

        let tu = ToolUse {
            id: "1".into(),
            name: "bash".into(),
            input: json!({"host": "  "}),
        };
        assert_eq!(resolve_host_for_call(&tu, "local").unwrap(), "local");
    }

    #[test]
    fn strip_host_removes_only_routing_field() {
        let mut tu = ToolUse {
            id: "1".into(),
            name: "bash".into(),
            input: json!({"command": "echo", "host": "devbox", "timeout_ms": 500}),
        };
        strip_host_field(&mut tu);
        assert_eq!(tu.input, json!({"command": "echo", "timeout_ms": 500}));
    }

    #[test]
    fn inject_host_field_adds_property() {
        let spec = generative_model::ToolSpec {
            name: "bash".into(),
            description: "x".into(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "command": { "type": "string" }
                }
            }),
        };
        let injected = inject_host_field(spec);
        assert!(
            injected.input_schema["properties"]["host"].is_object(),
            "{:?}",
            injected.input_schema
        );
        assert!(injected.input_schema["properties"]["command"].is_object());
    }

    #[test]
    fn standard_catalog_excludes_web_tools() {
        let names: Vec<_> = crate::host::HostWorker::standard_tool_specs()
            .into_iter()
            .map(|s| s.name)
            .collect();
        assert!(
            !names.iter().any(|n| n.starts_with("web_")),
            "remotes must not advertise web_* tools: {names:?}"
        );
        assert!(names.contains(&"bash".to_string()));
        assert!(names.contains(&"manual".to_string()));
        assert!(names.contains(&"lynx_tui_browser".to_string()));
    }

    #[test]
    fn lynx_tui_browser_is_standard_host_tool_with_routing_host() {
        let harness = Harness::local_with_services(Vec::new());
        let browser = harness
            .tool_specs()
            .into_iter()
            .find(|s| s.name == "lynx_tui_browser")
            .expect("lynx_tui_browser in standard catalog");
        let host = &browser.input_schema["properties"]["host"];
        assert!(
            host.is_object(),
            "lynx_tui_browser should get injected routing host: {host:?}"
        );
        let desc = host["description"].as_str().unwrap_or("");
        assert!(
            desc.contains("defaults to \"local\""),
            "expected routing host description, got {desc:?}"
        );
    }

    #[tokio::test]
    async fn root_only_tools_keep_host_field_and_run_local() {
        use crate::CancelToken;
        use crate::generative_model::Content;
        use crate::session::{ActiveSession, Session};
        use crate::tool_services::SessionMetaTool;

        let dir = std::env::temp_dir().join(format!(
            "myco-root-only-host-{}",
            crate::session::uuid_simple_hex(uuid::Uuid::new_v4())
        ));
        std::fs::create_dir_all(&dir).unwrap();
        unsafe {
            std::env::set_var("MYCO_HOME", &dir);
        }

        let active = ActiveSession::new(Session::new("claude-haiku-4-5"));
        let meta = Arc::new(SessionMetaTool::new(active.clone())) as Arc<dyn ToolService>;
        let harness = Harness::local_with_services(vec![meta]);

        // Schema must keep session_meta's own `host` (worktree links), not dual-purpose routing.
        let meta_spec = harness
            .tool_specs()
            .into_iter()
            .find(|s| s.name == "session_meta")
            .expect("session_meta advertised");
        let host_desc = meta_spec.input_schema["properties"]["host"]["description"]
            .as_str()
            .unwrap_or("");
        assert!(
            host_desc.contains("worktree") || host_desc.contains("Host name"),
            "session_meta host should describe worktree links, got {host_desc:?}"
        );
        // Routing-only description is only for multi-host tools.
        assert!(
            !host_desc.contains("defaults to \"local\""),
            "routing host description should not overwrite session_meta: {host_desc}"
        );

        let result = harness
            .clone()
            .dispatch_tool_use(
                ToolUse {
                    id: "t1".into(),
                    name: "session_meta".into(),
                    input: json!({
                        "action": "add_link",
                        "link_kind": "worktree",
                        "host": "devbox",
                        "path": "/tmp/wt",
                        "branch": "feat/x"
                    }),
                },
                Arc::new(crate::session::NullEventSink),
                TraceContext::default(),
                CancelToken::new(),
            )
            .await;
        assert!(!result.is_error, "{result:?}");
        let text: String = result
            .content
            .iter()
            .filter_map(|c| match c {
                Content::Text { text } => Some(text.as_str()),
                _ => None,
            })
            .collect();
        assert!(text.contains("devbox"), "{text}");
        assert!(text.contains("/tmp/wt"), "{text}");
        let links = active.snapshot().links;
        assert_eq!(links.len(), 1);

        let _ = std::fs::remove_dir_all(&dir);
        unsafe {
            std::env::remove_var("MYCO_HOME");
        }
    }

    #[tokio::test]
    async fn local_is_always_present_and_connected() {
        let harness = Harness::attach(HarnessConfig {
            enable_subagent: false,
            ..HarnessConfig::default()
        })
        .await
        .expect("attach");
        assert_eq!(harness.default_host(), "local");
        let status = harness.host_status();
        assert_eq!(status.len(), 1);
        assert_eq!(status[0].name, "local");
        assert!(status[0].connected);
        assert!(status[0].in_process);
        assert_eq!(status[0].command, vec!["in-process".to_string()]);

        let r = harness
            .clone()
            .dispatch_tool_use(
                ToolUse {
                    id: "t".into(),
                    name: "bash".into(),
                    input: json!({"command": "printf 'on-local\\n'"}),
                },
                Arc::new(crate::NullEventSink),
                TraceContext::default(),
                CancelToken::new(),
            )
            .await;
        assert!(!r.is_error, "{r:?}");
        let text = tool_text(&r);
        assert!(text.contains("on-local"), "{text}");
    }

    /// Attaching a harness must not index anything: auto-indexing is an
    /// explicit owner request, so tests never pay discovery/embedding work.
    #[tokio::test]
    async fn attach_does_not_auto_index() {
        let harness = Harness::attach(HarnessConfig {
            enable_subagent: false,
            ..HarnessConfig::default()
        })
        .await
        .expect("attach");

        // Give any (buggy) background indexing a chance to register roots.
        tokio::time::sleep(std::time::Duration::from_millis(50)).await;

        let r = harness
            .clone()
            .dispatch_tool_use(
                ToolUse {
                    id: "t".into(),
                    name: "indexed_exact_text_search".into(),
                    input: json!({"query": "anything"}),
                },
                Arc::new(crate::NullEventSink),
                TraceContext::default(),
                CancelToken::new(),
            )
            .await;
        assert!(r.is_error, "search must fail with empty index: {r:?}");
        assert!(
            tool_text(&r).contains("no directories indexed"),
            "{}",
            tool_text(&r)
        );
    }

    /// The owner's explicit request is what makes discovery targets
    /// (AGENTS.md & co.) searchable.
    #[tokio::test]
    async fn auto_index_local_is_owner_opt_in() {
        let dir = std::env::temp_dir().join(format!("myco-owner-index-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(
            dir.join("AGENTS.md"),
            "# guidance\nunique_owner_optin_token here\n",
        )
        .unwrap();

        let harness = Harness::attach(HarnessConfig {
            enable_subagent: false,
            ..HarnessConfig::default()
        })
        .await
        .expect("attach");
        harness.auto_index_local(dir.clone());

        // The crawl is a background task; poll until the root is searchable.
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);
        let hit = loop {
            let r = harness
                .clone()
                .dispatch_tool_use(
                    ToolUse {
                        id: "t".into(),
                        name: "indexed_exact_text_search".into(),
                        input: json!({"query": "unique_owner_optin_token"}),
                    },
                    Arc::new(crate::NullEventSink),
                    TraceContext::default(),
                    CancelToken::new(),
                )
                .await;
            if !r.is_error && tool_text(&r).contains("AGENTS.md") {
                break r;
            }
            if std::time::Instant::now() > deadline {
                panic!("owner-requested auto-index never became searchable: {r:?}");
            }
            tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        };
        assert!(
            tool_text(&hit).contains("unique_owner_optin_token"),
            "{hit:?}"
        );

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn multi_host_attach_and_route_by_host_field() {
        // Two remotes as local subprocesses (not SSH) to exercise routing without network.
        let program = myco_program();
        // Unit tests don't set CARGO_BIN_EXE_*; need a built binary. Skip rather
        // than flake on connect timeout when the binary is missing/stale.
        if program == "myco" || !std::path::Path::new(&program).is_file() {
            eprintln!("skip multi_host: no myco binary at {program:?} (cargo build --bin myco)");
            return;
        }
        let cfg = HarnessConfig {
            enable_subagent: false,
            // Host hello can be slow under parallel suite load (MiniLM seed).
            attach_timeout_secs: 60,
            remote_hosts: vec![
                HostConfig {
                    name: "a".into(),
                    command: vec![
                        program.clone(),
                        "--mode".into(),
                        "host".into(),
                        "--name".into(),
                        "a".into(),
                    ],
                    ssh_destination: None,
                },
                HostConfig {
                    name: "b".into(),
                    command: vec![
                        program,
                        "--mode".into(),
                        "host".into(),
                        "--name".into(),
                        "b".into(),
                    ],
                    ssh_destination: None,
                },
            ],
            ..Default::default()
        };
        let harness = Harness::attach(cfg).await.expect("attach");
        assert_eq!(harness.default_host(), "local");
        let status = harness.host_status();
        assert_eq!(status.len(), 3); // local + a + b
        let local = status.iter().find(|s| s.name == "local").unwrap();
        assert!(local.connected && local.in_process);
        assert!(
            status
                .iter()
                .filter(|s| s.name != "local")
                .all(|s| !s.connected)
        );

        // Default → local (in-process).
        let r = harness
            .clone()
            .dispatch_tool_use(
                ToolUse {
                    id: "t1".into(),
                    name: "bash".into(),
                    input: json!({"command": "printf 'on-local\\n'"}),
                },
                Arc::new(crate::NullEventSink),
                TraceContext::default(),
                CancelToken::new(),
            )
            .await;
        assert!(!r.is_error, "{r:?}");
        assert!(tool_text(&r).contains("on-local"), "{}", tool_text(&r));

        // Explicit host b (subprocess).
        let r = harness
            .clone()
            .dispatch_tool_use(
                ToolUse {
                    id: "t2".into(),
                    name: "bash".into(),
                    input: json!({"command": "printf 'on-b\\n'", "host": "b"}),
                },
                Arc::new(crate::NullEventSink),
                TraceContext::default(),
                CancelToken::new(),
            )
            .await;
        assert!(!r.is_error, "{r:?}");
        assert!(tool_text(&r).contains("on-b"), "{}", tool_text(&r));

        // Unknown host.
        let r = harness
            .dispatch_tool_use(
                ToolUse {
                    id: "t3".into(),
                    name: "bash".into(),
                    input: json!({"command": "true", "host": "nope"}),
                },
                Arc::new(crate::NullEventSink),
                TraceContext::default(),
                CancelToken::new(),
            )
            .await;
        assert!(r.is_error);
        assert!(tool_text(&r).contains("unknown host"), "{}", tool_text(&r));
    }

    #[tokio::test]
    async fn lazy_connect_failure_on_first_use() {
        let cfg = HarnessConfig {
            enable_subagent: false,
            attach_timeout_secs: 10,
            remote_hosts: vec![HostConfig {
                name: "ghost".into(),
                command: vec!["/nonexistent/myco-please-fail".into()],
                ssh_destination: None,
            }],
            ..Default::default()
        };
        let harness = Harness::attach(cfg)
            .await
            .expect("attach is lazy and does not probe remotes");
        let status = harness.host_status();
        let ghost = status.iter().find(|s| s.name == "ghost").unwrap();
        assert!(!ghost.connected, "{ghost:?}");
        assert!(ghost.error.is_none(), "no error until first use: {ghost:?}");

        let r = harness
            .clone()
            .dispatch_tool_use(
                ToolUse {
                    id: "t".into(),
                    name: "bash".into(),
                    input: json!({"command": "true", "host": "ghost"}),
                },
                Arc::new(crate::NullEventSink),
                TraceContext::default(),
                CancelToken::new(),
            )
            .await;
        assert!(r.is_error);
        let text = tool_text(&r);
        assert!(
            text.contains("ghost") || text.contains("spawn") || text.contains("No such"),
            "{text}"
        );
        let status = harness.host_status();
        let ghost = status.iter().find(|s| s.name == "ghost").unwrap();
        assert!(!ghost.connected, "{ghost:?}");
        assert!(ghost.error.is_some(), "{ghost:?}");
        // Local still fine.
        let local = status.iter().find(|s| s.name == "local").unwrap();
        assert!(local.connected && local.in_process);
    }

    fn tool_text(r: &generative_model::ToolResult) -> String {
        r.content
            .iter()
            .filter_map(|c| match c {
                generative_model::Content::Text { text } => Some(text.as_str()),
                _ => None,
            })
            .collect::<Vec<_>>()
            .join("\n")
    }
}