Justice David A. Lowy wrote that a judge has the power and discretion to determine probation requirements tailored to an individual and that further probation’s twin goals: rehabilitation and public safety. Judges, he said, “stand on the front lines of the opioid epidemic” and are “faced with difficult decisions that are especially unpalatable.”

The challenge was brought by Julie A. Eldred, now 30, who had been convicted of larceny for stealing jewelry to support her heroin habit. In August 2016, a district judge gave her a year’s probation, facing up to a 30-month sentence for violating conditions. The judge ordered her to begin outpatient treatment and remain drug-free. Ms. Eldred enrolled in a program and began taking Suboxone, a medication that can reduce cravings and symptoms of withdrawal. Shortly afterward, she relapsed and, she said, returned to her doctor for help, asking for a stronger dose of Suboxone.

Eleven days after her probation began, Ms. Eldred tested positive for fentanyl. The judge ordered her to go to inpatient treatment, but no placement could immediately be found. What should be done with her? It was the beginning of Labor Day weekend, and Ms. Eldred’s parents were out of town.

“The judge was faced with either releasing the defendant and risking that she would suffer an overdose and die or holding her in custody until a placement at an inpatient treatment facility became available,” Justice Lowy wrote, in the decision handed down Monday.

During the 10 days she was in a medium-security prison, Ms. Eldred went through withdrawal. Suboxone was not prescribed.