Chaos, artificial scarcity, and violence feeds the system and justifies its existence.
Otherwise, why would we still have a mass incarceration system? Why is it still punitive in nature with terrible and inhumane conditions normalized?
A cycle is created that makes people unemployable and industries and those in power reap the benefits at every stage of these people's lives - any police contact is effectively a scarlet letter. Specifically, many corporations benefit from the slave labor sourced from prisons and the private prison industry is its own can of worms.
With AI tooling screening job applicants with proprietary criteria, public data brokers, mass surveillance disguised as "adtech", people search websites, social media (where people have a tendency to overshare personal details), systematic reporting of arrest records/etc. in newspapers (generally with no updates to reflect the person's current situation); you can literally be unemployable in the US with no conviction or crimes that have been expunged or sealed.
If you have a felony or misdemeanor on your record - good fucking luck getting a job in today's market - background checks are normalized and are extremely accessible to employers. It's no wonder why people turn to crime to exist, discrimination is effectively legalized - there is insufficient regulation and protections for job applicants.
The only way to prevent crime is to rehabilitate those who commit crime and to provide services to enrich people's lives before they would otherwise commit crime. We also need to respect people's privacy upon rehabilitation - we shouldn't be permanently labeling (or dehumanizing) those deemed to be fit to return to society (e.g. people that aren't violent or who aren't a threat). We have to give them a path to participate in society.
What if locking people up indefinitely (as many were in institutions decades ago) and diagnosing them with subjective criteria isn't ideal? I'm not dismissing anybody's diagnosis or hand-waving real symptoms or illness - I'm merely suggesting that an authoritarian system where human rights are stripped with minimal outside observation (with sometimes flimsy criteria and fallible actors) is potentially damaging to mental health and is probably not conducive to healing. It can be a very imbalanced power dynamic, especially as it was in the institutions of the past as you pointed out.
We need an answer to retain the rights of those involuntarily held as best as possible. I think it's important to make courts more accessible to patients (and their loved ones), providing those held involuntarily with access to second opinions or different facilities (in some cases), and having established (and independently enforced) criteria for release - with appeals available for patients to argue their case for release with legal representation and other expert witnesses (e.g. other psychiatrists, qualified individuals directly involved in their care past or present) and perhaps even family members and other people who were involved with the patient.
Involuntary commitment (for any extended period) should be reserved for the severely mentally ill, who are determined by independent review to be in need of treatment to stabilize - and only those who are a danger to themselves or others, those who committed crimes, and those who are actively violent should be held in higher-security (locked) facilities.
I feel the rest would benefit greatly from conditions akin to a Soteria House (without locked doors, forced medication, or coercion) - the Soteria House model could be expanded, adapted, or modified. Treatment could be loosely mandated by courts, with reviews conducted and alternative treatment plans established if the patient wishes to modify or discontinue treatment before they are thought to be stabilized by their psychiatrist(s) and care team. I feel that maintaining consent, valuing patient input in forming treatment plans, and avoiding coercion is key to address certain states of trauma - otherwise patients are potentially faced with more trauma.
For those who are not thought to be severely ill, but who are thought to be in temporary crisis (and who are not thought to be violent or a threat to themselves or others), stabilization could be attempted in a temporary hold to assess their state, and continued onward with care akin to Soteria Houses or intensive outpatient care and other forms of observation and forms of support (e.g. with their environment and other distressing situations they are facing).
And to respond directly to you, I definitely feel like society was incapable or very underequipped to fix the institutions back then. Society is still largely unable to address distress and its very real manifestations or consequences - such as homelessness and the prevention of individuals from becoming homeless against their will.