I agree that the problem extends beyond the attacker, but it's also beyond the criminal justice systems. The country doesn't take care of mentally ill people (at least the poorer ones), so the cycles of mentally ill people going through the system is pretty messed up. It seems like they'll never get meaningful help, and they'll only get locked up once they commit a serious enough crime. If as a society, we're not going to help people like this before they do something terrible, then it's on all of us. Would we rather pay for someone to get treatment before they commit a crime, or for a the trial and incarceration after? We're paying for it either way.
https://www.npr.org/2017/11/30/5674...atric-hospitals-led-to-a-mental-health-crisis
And for some broader context:
https://en.wikipedia.org/wiki/Deinstitutionalisation#United_States