Principal among the lingering problems Judge Lawrence Karlton singled out were severe staffing shortages at Salinas Valley's psychiatric unit in Soledad, which according to staff there reached "crisis level" months ago.

The state has not proved its mentally ill inmates "no longer face substantial risk of serious harm due to significant shortages in mental health staffing," Karlton wrote.

"Chronic understaffing is a central part of the ongoing constitutional violation in this action."

Gov. Jerry Brown filed a motion in January to end the decades-long class-action lawsuit that led to federal oversight of mental health care in state prisons and more recently to a U.S. Supreme Court order to lower California's prison population.

At one point chastising state officials for not behaving like "grown-ups," Karlton said state attorneys committed ethics violations when their experts interviewed mentally ill inmates at Salinas Valley during what inmates' attorneys called "secret prison inspections."

Karlton said he would refer his opinion on the ethics violations to the California Bar Association and to state Attorney General Kamala Harris.

In his ruling, the judge said even the state has admitted to ongoing staffing shortages at Salinas Valley's psychiatric program, which is operated by the California Department of State Hospitals.

Earlier this year, nine psychiatrists at the prison sent letters to the state hospitals department warning that the serious shortages were creating dangerous conditions for patients and staff. Other staff members have said patients lack even basic hygiene items such as soap or clean underwear and bedding.

California Department of Corrections and Rehabilitation officials said the state plans to appeal Karlton's ruling.

"It's unfortunate that the judge didn't give the appropriate weight to reports by national experts who found that CDCR is providing constitutional mental health care to inmates, and in fact, is a model for the nation," corrections department spokeswoman Deborah Hoffman wrote in an emailed statement.

"There was nothing unethical about their evaluations," Hoffman said. "We will appeal this decision and are confident that we will prevail. It's time for this costly and intrusive lawsuit — now in its twenty-third year — to come to an end."

Attorney Michael Bien, who represents mentally ill inmates in the class-action suit, said it wouldn't have dragged on so long if the state had acted more quickly to resolve the problems that led to federal oversight.

"I think it's a thoroughly comprehensive loss for the state," Bien said shortly after Karlton issued his ruling about 3 p.m. "They set out to establish that they fixed all the problems, that they've done a wonderful job. The judge rejected every one of those contentions."

Karlton said the state has yet to do enough to prevent patient suicides, which occur at rates exceeding the national average.

Bien said a suicide that occurred in November at the Salinas Valley mental health unit is an example of the problems he plans to ask officials to address in coming weeks.

The inmate hanged himself Nov. 29 and died several days later.

Prison staff "had not done clinical work to get him to receive treatment, or to be able to leave his cell," Bien said.

"You can't lock him in his cell for several weeks" while awaiting treatment, Bien said. "They need to move quicker. Lives have been lost."

Karlton wrote about another patient, a 37-year-old man called "Inmate H," who hanged himself three days after he was discharged from the Salinas Valley program to another prison with a lower level of care.

A suicide risk evaluation of the man performed at Salinas Valley "failed to recognize" a previous suicide attempt, Karlton wrote. He said the corrections department's own suicide reviewer "acknowledged the inadequacies" of the man's evaluation, which was conducted without a Spanish-language interpreter and without "more than a cursory record review."

Bien said upcoming motions to the court will ask for "administrative relief" to address these and myriad other concerns, many of which were uncovered as the case progressed.

Ernest Galvan, Bien's co-counsel, said in a statement Friday, "The best way for the state to respond to today's order is to make every effort to fix the many parts of their system that remain broken. When they have really fixed the system, then they should ask the court to revisit ending this case."