I think you cannot get the UserAgent correctly from the session directly. I have your wish functionally working using the following setup.
If you create a JAVA action with the Session as input parameter and you call it using the $currentSession variable, this Java should work:
// BEGIN USER CODE
IContext ctx = SessionParameter1.getContext();
Optional<IMxRuntimeRequest> orreq = ctx.getRuntimeRequest();
Optional<IMxRuntimeResponse> orres = ctx.getRuntimeResponse();
String UserAgent = null;
if (orreq.isPresent()) {
IMxRuntimeRequest rr = orreq.get();
UserAgent = rr.getHeader("User-Agent");
}
return UserAgent;
// END USER CODE