Skip to main content

agentmux_srv/server/remote_ops/
move_harness.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Whether the agent's harness (its CLI) can run on this computer (SPEC_MOVE_AN_AGENT_TO_ANOTHER_COMPUTER section 7, rows c and
5//! 7.3 "missing CLI"), judged once when the offer arrives from the harness the signed offer names. A harness this computer has
6//! needs no word. One it does not have but installs itself is said in what the person decides on and installs at the agent's first
7//! start (npm ones; the others are installed from the Toolchain pane first, and the words say so). One this computer cannot have
8//! at all (a harness it does not know, or a custom one that is not here) refuses the offer before anything is sent, naming it.
9
10use crate::backend::providers::{get_provider, ProviderConfig};
11
12/// What the harness the offer names comes to on this computer.
13#[derive(Debug, PartialEq, Eq)]
14pub(crate) enum HarnessCall {
15    /// The offer names none (an older source), or the CLI is here.
16    Fine,
17    /// The CLI is not here and is installed at the agent's first start, or first by hand; said to the person deciding.
18    Installs(String),
19    /// This computer cannot have the CLI: the offer is refused.
20    Unavailable(String),
21}
22
23/// The judgement, from what is known: the provider (when this computer knows the id), whether its CLI is here, and whether
24/// AgentMux installs it. Pure, so the three answers are tested without a CLI on disk.
25pub(crate) fn judge(harness: &str, provider: Option<&ProviderConfig>, located: bool, installable: bool) -> HarnessCall {
26    let Some(provider) = provider else {
27        return HarnessCall::Unavailable(format!("it runs on {harness}, a harness this computer does not know"));
28    };
29    if located {
30        return HarnessCall::Fine;
31    }
32    if !installable {
33        return HarnessCall::Unavailable(format!(
34            "it runs on {harness}, whose CLI ({}) is not on this computer and is not one AgentMux installs: install it here first",
35            provider.cli_command
36        ));
37    }
38    if provider.npm_package.is_empty() {
39        HarnessCall::Installs(format!("{harness} ({}) is not installed here: install it from the Toolchain pane before the agent can start", provider.cli_command))
40    } else {
41        HarnessCall::Installs(format!("{harness} ({}) is not installed here: it is installed when the agent first starts", provider.cli_command))
42    }
43}
44
45/// Judges the harness the signed offer names against this computer.
46pub(crate) async fn harness_call(harness: &str) -> HarnessCall {
47    let harness = harness.trim();
48    if harness.is_empty() {
49        return HarnessCall::Fine;
50    }
51    let provider = get_provider(harness);
52    let located = match provider {
53        Some(p) => crate::server::cli_handlers::locate_provider_cli(p.id).await.is_some(),
54        None => false,
55    };
56    let installable = crate::server::install_handlers::installable_provider(harness).is_ok();
57    judge(harness, provider, located, installable)
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    fn claude() -> &'static ProviderConfig {
65        get_provider("claude").expect("claude is a provider")
66    }
67
68    #[test]
69    fn a_cli_that_is_here_needs_no_word() {
70        assert_eq!(judge("claude", Some(claude()), true, true), HarnessCall::Fine);
71    }
72
73    #[test]
74    fn a_harness_that_is_not_here_but_is_installed_by_agentmux_is_said_not_refused() {
75        match judge("claude", Some(claude()), false, true) {
76            HarnessCall::Installs(note) => {
77                assert!(note.contains("not installed here"), "{note}");
78                assert!(note.contains("claude"), "{note}");
79            }
80            other => panic!("{other:?}"),
81        }
82    }
83
84    #[test]
85    fn an_unknown_harness_or_one_agentmux_does_not_install_refuses_naming_it() {
86        match judge("no-such-harness", None, false, false) {
87            HarnessCall::Unavailable(why) => assert!(why.contains("no-such-harness"), "{why}"),
88            other => panic!("{other:?}"),
89        }
90        match judge("claude", Some(claude()), false, false) {
91            HarnessCall::Unavailable(why) => assert!(why.contains("install it here first"), "{why}"),
92            other => panic!("{other:?}"),
93        }
94    }
95
96    #[tokio::test]
97    async fn an_offer_that_names_no_harness_is_not_judged() {
98        assert_eq!(harness_call("").await, HarnessCall::Fine);
99        assert_eq!(harness_call("   ").await, HarnessCall::Fine);
100        assert!(matches!(harness_call("no-such-harness").await, HarnessCall::Unavailable(_)));
101    }
102}