Thanks for pointing that out; I was not aware of it.
At the same time, when I helped with research in a cognitive psychology lab in school (much closer to the present than to that law), our PI said our funding came partly from a grant from the US Navy. We worked on Bayesian models of human learning and reasoning -- so "direct or apparent relationship to specific military function" has perhaps at times been interpreted somewhat loosely?
As a practical matter, I'm not sure the Mansfield amendment affected all agencies equally. My impression is that it affected ARPA (later DARPA) most profoundly. (pretty much everything I've read about this was in the context of ARPA and XEROX Parc so my perception might be a little skewed)
I'd speculate the Navy had perhaps more discretion over how it spent its research funding. For example, they funded polywell fusion research for years.